Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Using and building own middleware in Laravel

Tuesday, March 17th, 2015 by Servage

middlewareWith Laravel 5 there are new possibilities using middleware. Essentially HTTP middleware is taking any request and doing “something” with it. For example Laravel has native middleware that can check authentication and authorization, so the framework can determine if the user is logged in and may perform a given action. If not, the middleware can redirect the user to a login page or display a relevant message. If the user is allowed, the middleware will let the request proceed without intervention.

Another example could be to check for SSL encryption status. Some pages may require unencrypted requests (http), some may require encrypted requests (https), and some may work with both. This depends on the content and session handling you wish to achieve. Middleware could be implemented to check the current SSL status against a setting indicating the current requirement. The middleware could then either enforce the correct HTTP/HTTPS status via redirection, or show a relevant message.

You could also use middleware to perform some general checks on all input data, or inject information into all response headers. The possibilities are numerous, and middleware generally speaking simply provides a centralized way of manipulating requests and their responses systematically.

Laravel comes with some ready-made middleware. It is located in the app/Http/Middleware folder. It should provide you for the most basic needs, while for more complicated requirements you will need to create your own middleware. Fortunately this is not very complicated, as described below.

Using middleware in routes

You can define which middleware to use in the routes configuration. For example like below:

Route::get('user/profile', ['middleware' => 'auth', function()
    // Do something here ...

This will tell Laravel to use the auth middleware for the user/profile route.

Building own middleware

Laravel comes with plenty of utilities to make common processes simple. This is also valid for creating own middleware. Use the following artisan command to get started:

php artisan make:middleware MyMiddleware

The command above creates a script file for MyMiddleware in app/Http/Middleware.

Terminable middleware

Middleware can also be specified to execute after the response has been created. This is advantageous if you need to process the finished response – or just do something after the request has finished, and avoid slowing down the users experience and load time.

Compartmentalize with middleware

One great advantage of middleware is the previously mentioned ability to hook into every request and perform some actions on it. However, the middleware structure also forces developers to think in small pieces of functionality, which is a great method for compartmentalization of code. Building small maintainable blocks of code with a specific and limited function is great for projects with a long lifespan. Future developers (even yourself!) will have a greater time understanding and adapting code over time.

Compartmentalization can be achieved easily by splitting large code files and spaghetti-code into multiple classes and methods – hence reducing the required time to grasp a specific code segment. Therefore you should always consider how you may be able to split larger segments of code into more digestible portions. If you then add proper documentation to your code, you have built a pretty great foundation for future use of it.

Using and building own middleware in Laravel, 4.2 out of 5 based on 5 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.