Skip to main content

Laravel – Middleware

Middleware acts as a bridge between a request and a response. It is a type of filtering mechanism. This chapter explains you the middleware mechanism in Laravel.
Laravel includes a middleware that verifies whether the user of the application is authenticated or not. If the user is authenticated, it redirects to the home page otherwise, if not, it redirects to the login page.
Middleware can be created by executing the following command −
Replace the <middleware-name> with the name of your middleware. The middleware that you create 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
Step 2 − After successful execution of the command, you will receive the following output −
Step 3 − AgeMiddleware will be created at app/Http/Middleware. The newly created file will have the following code already created for you.

Registering Middleware

We need to register each and every middleware before using it. There are two types of Middleware in Laravel.
  • Global Middleware
  • 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$middlewareproperty is used to register Global Middleware and $routeMiddlewareproperty is used to register route specific middleware.
To register the global middleware, list the class at the end of $middleware property.
To register the route specific middleware, add the key and value to $routeMiddleware property.

Example

We have created AgeMiddleware in the previous example. We can now register it in route specific middleware property. The code for that registration is shown below.
The following is the code for app/Http/Kernel.php −

Middleware Parameters

We can also pass parameters with the Middleware. For example, if your application has different roles like user, admin, super admin etc. and 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 $nextargument.

Example

Step 1 − Create RoleMiddleware by executing the following command −
Step 2 − After successful execution, you will receive the following output −

Age middleware
Age middleware
Step 3 − Add the following code in the handle method of the newly created RoleMiddlewareat app/Http/Middleware/RoleMiddleware.php.
Step 4 − Register the RoleMiddleware in app\Http\Kernel.php file. Add the line highlighted in gray color in that file to register RoleMiddleware.
rolemiddleware
rolemiddleware
Step 5 − Execute the following command to create TestController −
Step 6 − After successful execution of the above step, you will receive the following output −
testcontroller
testcontroller
Step 7 − Copy the following lines of code to app/Http/TestController.phpfile.
app/Http/TestController.php
Step 8 − Add the following line of code in app/Http/routes.php file.
app/Http/routes.php
Step 9 − Visit the following URL to test the Middleware with parameters
Step 10 − The output will appear as shown in the following image.
Role Editor

Terminable Middleware

Terminable middleware performs some task after the response has been sent to the browser. This can be accomplished 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. Terminate method can be created as shown in the following code.

Example

Step 1 − Create TerminateMiddleware by executing the below command.
Step 2 − The above step will produce the following output −
terminable_middleware
terminable_middleware
Step 3 − Copy the following code in the newly created TerminateMiddleware at app/Http/Middleware/TerminateMiddleware.php.
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.
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
Step 8 − Add the following line of code in app/Http/routes.php file.
app/Http/routes.php
Step 9 − Visit the following URL to test the Terminable Middleware.
Step 10 − The output will appear as shown in the following image.

Popular posts from this blog

What is Lumen?

What is Lumen? Lumen  is PHP micro-framework that built on Laravel’s top components.It is created by Taylor Otwell. It is perfect option for building Laravel based micro-services and fast REST API’s. It’s one of the fastest micro-frameworks available. You can install Lumen using composer by running below command 1 composer create - project -- prefer - dist laravel / lumen blog

Laravel – Response

Laravel – Response A web application responds to a user’s request in many ways depending on many parameters. This chapter explains you in detail about responses in Laravel web applications. Basic Response Laravel provides several different ways to return response. Response can be sent either from route or from controller. The basic response that can be sent is simple string as shown in the below sample code. This string will be automatically converted to appropriate HTTP response. Example Step 1  − Add the following code to  app/Http/routes.php  file. app/Http/routes.php 1 2 3 Route :: get ( '/basic_response' , function ( ) {    return 'Hello World' ; } ) ; Step 2  −  Visit  the following URL to test the basic response. 1 http : //localhost:8000/basic_response Step 3  − The output will appear as shown in the following image. Attaching Headers The response can be attached t...

Which template engine Laravel use ?

Which template engine Laravel use ? Laravel uses Blade Templating Engine. Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php file extension and are typically stored in the resources/views directory.