Laravel Controllers

Controllers are another important 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.

Creating Controllers

Open your CMD or terminal and type the command:

Syntax:
php artisan make:controller <controller-name> --plain

Replace this <controller-name> in the above syntax with your controller. This will eventually make a plain constructor since you are passing the argument --plain.

The controller that you have created can be invoked from within routes.php file using this syntax below-

Example:
Route::get('base URI','[email protected]');

A basic controller code-snippet will look something like this, and you have to create in the directory like app/Http/Controller/AdminController.php:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class AdminController extends Controller
 {
 //
 }

Controller Middleware

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

Example:
Route::get('profile', '[email protected]')->middleware('auth');

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

class AdminController extends Controller
{
    public function __construct()
    {
        // function body
    }
}

Using closures, it is possible that controllers allow laravel developers in registering middleware.

$this->middleware(function ($request, $next) {
    // middleware statements;
    return $next($request);
}
);

Resource Controllers

The resource route of Laravel allot 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 -

Route::resources([
    'password' => 'PasswordController',
    'picture' => 'DpController'
]
);

Actions Handled by Resource Controllers

VerbURIActionRoute Name
GET/usersUsers listusers.index
POST/users/addAdd new user users.add
GET/users/{user}Get user users.show
GET/users/{user}/editEdit user users.edit
PUT/users/{user}Update user users.update
DELETE/users/{user}Delete user users.destroy

Implicit Controllers

These type of controllers allow developers in defining a single route for handling multiple actions within the controller. The syntax of using this is by:

Example:
Route::controller('base URI','<class-name-of-the-controller>');

This is where your Implicit controller file will get stored: app/Http/Controllers/ImplicitController.php; and will look have to script like:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ImplicitController extends Controller {
   public function getIndex(){
      echo 'starting method';
   }
   public function getVal($id){
      echo 'show value';
   }
   public function getAdminData(){
      echo 'admin data method';
   }   
   public function adminPassword(){
      echo 'password method';
   }
}

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.


Scroll Back to Top