Laravel Tutorial

Controllers are another essential feature provided by Laravel, where in place of defining the handling request logic in the form of Closures in route files, it is possible to organize this process with the help of Controller classes. So what the controllers do? Controllers are meant to group associated request handling logic within a single class. In your Laravel project, they are stored in the app/Http/Controllers' directory. The full form of MVC is Model View Controller, which act as directing traffic among the Views and the Models.

Controller Middleware

You can assign controllers to middlewares to route in the route files of your project using the command below:

Example:

Route::get('profile', 'AdminController@show')->middleware('auth');

Middleware methods from the controller help to easily assign middleware to the controller's action and activity. Restrictions on implementing certain methods can also be provided to middlewares on the controller class.

Resource Controllers

The resource route of Laravel allows the classic "CRUD" routes for controllers having a single line of code. This can be created quickly using the make: controller command (Artisan command) something like this"

Example:

php artisan make:controller PasswordController --resource

The above code will produce a controller in app/Http/Controllers/ location with file name PasswordController.php which will hold a method for all available tasks of resources.

Laravel developers also have the freedom to register multiple resource controllers at a time by passing an array to resource method something like this -

Constructor and Method Injection

The service container of Laravel is used for resolving all Laravel Controllers. So, controller injection lets Laravel developers to type-hint the dependencies which your controller may require within its constructor. On the other hand, method injection allows you to type-hint dependencies for the controller's action method in your Laravel project.