Understanding basics of Laravel 5 middleware

Understanding basics of laravel 5 middleware

Basics of Laravel 5 middleware

Middleware is a type of filtering mechanism that acts as a bridge between a request and a response. Here we are going to understand the middleware mechanism in Laravel 5. If the user is authenticated, it redirects to the home page otherwise, if not, it redirects to the login page.

php artisan make:middleware

Replaced with the name of your middleware. The middleware that you created can be seen at app/Http/Middleware directory.

 

Example-
Observe the following example to understand the middleware mechanism −

Step 1 − Let us now create AgeMiddleware. To create that, we need to execute the following command −
php artisan make:middleware AgeMiddleware

Step 2 − After successful execution of the command, you will receive the following output − “Middleware created successfully”.

Step 3 − AgeMiddleware will be created at app/Http/Middleware. The newly created file will have the following code already create for you.



namespace App\Http\Middleware;
use Closure;
class AgeMiddleware {
public function handle($request, Closure $next) {
return $next($request);
}
}

 

Registering the Middleware

We need to registered each and every middleware before using it. There are two types of Middleware in Laravel.
1. Global Middleware
2. Route Middleware

 

The Global Middleware will run on every HTTP request of the application, whereas the Route Middleware will be assigned to a specific route. The middleware can be registered at app/Http/Kernel.php. This file contains two properties $middleware and $routeMiddleware. $middleware property is used to register Global Middleware and $routeMiddleware property is used to register route specific middleware.

To register the global middleware, list the class at the end of $middleware property.

protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
];

 

Example
We have created AgeMiddleware in the previous example. We can now registered it into route specific middleware property. The code for that registration is shown below.

The following is the code for app/Http/Kernel.php −



'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'Age' => \App\Http\Middleware\AgeMiddleware::class,
];
}

 

Middleware Parameters

We can also pass parameters with the Middleware. You want to authenticate the action based on role, this can be achieved by passing parameters with middleware. The middleware that we create contains the following function and we can pass our custom argument after the $next argument.


public function handle($request, Closure $next) {
return $next($request);
}

Terminable Middleware

Terminable middleware performs some task after the response has been sent to the browser. This can be accomplish by creating a middleware with terminate method in the middleware. Terminable middleware should be registered with global middleware. The terminate method will receive two arguments $request and $response. The Terminate method can be created as shown in the following code.

Example

Step 1 − Create TerminateMiddleware by executing the below command.

php artisan make:middleware TerminateMiddleware

Step 2 − The above step will produce the following output −

Step 3 − Copy the following code in the newly created TerminateMiddleware at app/Http/Middleware/TerminateMiddleware.php.



public function handle($request, Closure $next) {
echo "Executing statements of handle method of TerminateMiddleware.";
return $next($request);
}

public function terminate($request, $response) {
echo ”
Executing statements of terminate method of TerminateMiddleware.”;
}
}

Step 4 − Register the TerminateMiddleware in app\Http\Kernel.php file. Add the line highlighted in gray color in that file to register TerminateMiddleware.

Step 5 − Execute the following command to create ABCController.

php artisan make:controller ABCController --plain

Step 6 − After the successful execution of the URL, you will receive the following output −

Step 7 − Copy the following code to app/Http/ABCController.php file.


app/Http/ABCController.php

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ABCController extends Controller {
public function index() {
echo ”
ABC Controller.”;
}
}

Step 8 − Add the following line of code in app/Http/routes.php file.


app/Http/routes.php

Route::get(‘terminate’,[
‘middleware’ => ‘terminate’,
‘uses’ => ‘ABCController@index’,
]);

Step 9 − Visit the following URL to test the Terminable Middleware.

http://localhost:8000/terminate

Step 10 − The output will appear as shown in the following image.