Laravel Middleware - Understanding Middleware | W3schools

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).

‘User’ => \App\Http\Middleware\UserMiddleware::class,

Also Read: Laravel Controller

Step 5: Now create a controller under the name NewController. Use the following code for the creation of the new controller.

laravel> php artisan make:controller NewController –plain

[post_middile_section_ad]

Step 6: Once you are through the successful creation of the new controller then copy the below code at app/Http/NewController.php.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Controllers\Controller;

class NewController extends Controller

{

   public function index()

{

      echo “<br> New Controller.”;

 }

} 

Step 7: Now add the following code to the app/Http/routes.php file.

Route::get(‘role’,[

‘middleware’ => ‘Role:editor’,

‘uses’ => ‘NewController@index’,

]);

Step 8: After this, you just need to type in the provided URL to test the Middleware with parameters.

http://localhost:8000/role

Step 9: The output will appear like the following.

Role:editor

New Controller

Terminable Middleware

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.

<?php

namespace App\Http\Middleware;

use Closure;

class TerminateMiddleware {

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

      echo “Executing statements of handle method of TerminateMiddleware.”;

      return $next($request);

   }

   public function terminate($request, $response){

      echo “<br>Executing statements of terminate method of TerminateMiddleware.”;

   }

}

Step 3: To register this newly created handle, open the file in app\Http\Kernel.php. In this file, add the below code to the $routeMiddleware class (protected).

‘terminate’ => \App\Http\Middleware\TerminateMiddleware::class,

Step 4: Run the below command to create a controller named as ABCController

laravel> php artisan make:controller ABCController –plain

Step 5: After the successful creation of the controller, add the following code to app/Http/ABCController.php file.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Controllers\Controller;

class ABCController extends Controller

{

public function index()

{

echo “<br>ABC Controller.”;

}

}

Step 6: Now, it’s time to add a line in the coded file of app/Http/routes.php.

Route::get(‘terminate’,[

   ‘middleware’ => ‘terminate’,

   ‘uses’ => ‘ABCController@index’,

]);

Step 7: Test the URL for the Terminable URL.

http://localhost:8000/terminate

Step 8: The output will appear like the following.

Executing statements of handle method of TerminateMiddleware.

ABC Controller.

Executing statements o the terminate method of TerminateMiddleware.