Middleware in Laravel

Code With Travel
2 min readMar 18, 2018

--

HTTP Middlewares provide a filter on HTTP requests entering your application. Laravel, for example, has a middleware for verifying a user’s authentication.

What is Middleware?

As the name suggest, Middleware acts as a middle man between request and response. It’s like if you want something to occur before or after the code that handles the request is run, you would put in a middleware.

When to use middleware?

  • Rate-limiting a service call.
  • Using a middleware to confirm the incoming route request API key. Assuming you are building an API.
  • Change the site language based on locale.

In this article you should be able to create middleware , registers it and use it on your project.

1.) Creating middleware

artisan, creating middlewares in Laravel is easy. Open a terminal in the project root and run the following command.

php artisan make:middleware <MiddlewareName>

2.) Registering a Middleware

Now we have created middleware, let’s register it. If you want a middleware to run on every request, go to app/Http/kernel.php and add the middleware to Kernel class $middlewareproperty.

protected $middleware = [
...
\App\Http\Middleware\<MiddlewareName>::class
];

If you want the middleware to trigger on some routes, we can name the middleware and use that as a reference mechanism to add it to some routes. To name the middleware, while still in the app/Http/kernel.php, add the keyed property to the $routeMiddleware array. The array key is the name of the middleware, while the value should be the path of the middleware.

protected $routeMiddleware = [
...
'<Name of Middleware>' => \App\Http\Middleware\<MiddlewareName>::class,
...
];

3.) Attach middleware to routes

Let’s take example.

Route::get('posts/{something}', 'PostController@getPost');

Here getPost method on the PostController class fires when the url matches posts/{something}

In this We could add our middleware by changing the second parameter of Route::get

Route::get('posts/{something}', ['middleware' => '<Name of Middleware>', function () {
return "First middleware.";
}]);

Another to add middleware to routes is to call a middleware method on the route definition. Like this.

Route::get('posts/{something}', function () {
//
})->middleware(['first', 'second']);

4.) Middleware Parameters

Passing parameters to middlewares is quite easy. Say, for example, our middleware validates the role of a user before allowing access to a page. We could also pass a list of allowed roles to the middleware.

To pass a parameter to our middleware, after the $request and Closureparameters,

public function handle($request, Closure $next, $role)
{
if (! $request->user()->hasRole($role)) {
// Redirect...
}

return $next($request);
}

Now attach middleware

Route::post('post/{id}', ['middleware' => 'role:editor', function ($id) {
//
}]);

If we want to first execute our middleware then we perform our action and return the response.They we can do like this.

public function handle($request, Closure $next)
{
$response = $next($request);

/**
* Perform actions here
*/

return $response;
}

Conclusion

You can create a middleware to do so much more than what I have listed above.

--

--

No responses yet