Middleware

Middleware acts as a filter between the request and the response. For an instance, A middleware of Laravel is used to verify the authenticated users. An authenticated user will get redirected to the dashboard otherwise, the user will remain on the same page.

Command to create middleware:

laravel> php artisan make:middleware <middleware-name>

You need to name the middleware in the <middleware-name> field. This middleware will be seen in the app directory.

Take this example for instance,

[post_middile_section_ad]

Step 1: Create a middleware with the name ‘IDMiddleware’. For this, execute the following command.

laravel> php artisan make:middleware IDMiddleware

Step 2: You will get the output after the successful execution of the above command.

Registration of Middleware

To use a middleware, one needs to register it first. The middleware will be registered under app/Http/Kernel.php.The registration process can be done with the following methods.

* Global Middleware

The global middleware is capable of running on any of the HTTP requests of the application. To register a global middleware, you need to use $middleware property.

* Route Middleware

The route middleware can run on the specified route of the application. To register a route middleware, you will be using $routeMiddleware.

Middleware Parameters

You can also pass parameters within the middleware. This can be used in several conditions. For example, an application is having a number of users which include admin, different users, supervising admin, and a few others. In such a condition, you will be required to pass the parameters so as to identify and authenticate every user. The parameters can be passed within the middleware with the help of $next argument.

public function handle($request, Closure $next)

{

return $next($request);

}

To better understanding of the above section, go through the below example.

Step 1: Create a middleware by the name UserMiddleware. Use the following command.

laravel> php artisan make:middleware UserMiddleware

Step 2: Execute the command successfully.

Step 3: Open the recently created file. You will find this new handle under the given path: app/Http/Middleware/UserMiddleware.php. Add the below code to this file.

<?php

namespace App\Http\Middleware;

use Closure;

class UserMiddleware

{

public function handle($request, Closure $next, $role)

{

echo “Role: “.$role;

return $next($request);

}

}

Step 4: You will be needed to register this newly created middleware at app\Http\Kernel.php. To register, type in given code line in the already existing $routeMiddleware class (protected).

The terminable middleware starts performing only after the browser receives a response. To get a terminable middleware, you need to create a ‘terminable’ method in the global middleware. The method will have two arguments, which are $request and $response.

The following example will help you in creating a terminable middleware.

Step 1: Creation of the TerminateMiddleware by the use of the following command.

laravel> php artisan make:middleware TerminateMiddleware

Step 2: After the successful execution of the above command, copy the given code in the app/Http/Middleware/TerminateMiddleware.php file.