CONTROLLERS

  • In Laravel, a controller is a crucial component used to handle HTTP requests and define the application’s response logic. Controllers serve as an intermediary between routes and the actual business logic of your application.

Controllers

  • Instead of defining all of your request handling logic as closures in your route files, you may wish to organize this behavior using “controller” classes.
  • Controllers can group related request handling logic into a single class.
  • For example, a UserController class might handle all incoming requests related to users, including showing, creating, updating, and deleting users.
  • By default, controllers are stored in the app/Http/Controllers directory.

Writing Controllers

Basic controllers –

To quickly generate a new controller, you may run the make:controller Artisan command. By default, all of the controllers for your application are stored in the app/Http/Controllers directory:

php artisan make:controller UserController

  • You can define a route to this controller method like so: use App\Http\Controllers\UserController; Route::get(‘/user/{id}’, [UserController::class, ‘show’]);
  • When an incoming request matches the specified route URI, the show method on the App\Http\Controllers\UserController class will be invoked and the route parameters will be passed to the method.
  • Single Action Controllers – If a controller action is particularly complex, you might find it convenient to dedicate an entire controller class to that single action. To accomplish this, you may define a single invoke method within the controller:

<?php

namespace App\Http\Controllers; use App\Http\Controllers\Controller;

class ProvisionServer extends Controller

{

public function _ _invoke()

{

// …

}

}

  • When registering routes for single action controllers, you do not need to specify a controller method. Instead, you may simply pass the name of the controller to the router:

use App\Http\Controllers\ProvisionServer;

Route::post(‘/server’, ProvisionServer::class);

You may generate an invokable controller by using the –invokable option of the make:controller Artisan command:

php artisan make:controller ProvisionServer —

invokable

Dependency Injection & Controllers

  • A common use-case for method injection is injecting the Illuminate\Http\Request instance into your controller methods:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller

{

public function store(Request $request)

{

$name = $request->name;

//

}

}

Dependency Injection

  • It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.

  • If your controller method is also expecting input from a route parameter, list your route arguments after your other dependencies. For example, if your route is defined like so:

use App\Http\Controllers\UserController; Route::put(‘/user/{id}’, [UserController::class, ‘update’]);

  • You may still type-hint the Illuminate\Http\Request and access your id parameter by defining your controller method as follows:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller

{

public function update(Request $request, $id)

{

//

}

}

 

[pdf_note link=”https://drive.google.com/file/d/1Bm7vhOiE9Iq1hEJLFvyrUWLLCu01-b_8/view”]