Laravel 5: HTTP Layer – Middleware

Laravel 5: HTTP Layer – Middleware

Middleware provide a convenient mechanism for dealing with HTTP requests entering your application (before middleware), or responses leaving it (after middleware).

If you have some jobs to be done before request reaches resource, or after response leaves the system, you can use middleware.

Green spots indicate some action performed by middleware.

Most obvious use for middleware is authentication.
When you check if user is logged in, so he can see some areas for logged user, you use middleware.

Other uses can include:

  • logging entries
  • sending emails
  • attaching headers to response etc.

Middleware is located in app/Http/Middleware directory.

Create Middleware Scaffolding

Artisan has a handy command to create middleware for us:

Middleware will be created inside of folder: app\Http\Middleware:


Middleware class has a very simple structure. All action happens in method handle():

see middleware before/after for proper composition of method handle().

Middleware Before and After

Sometimes, you want your middleware to fire, before request reached resource it is aiming at.

There are times, when you want your middleware to act, when resource did its job and response is on the way to leave your system.

First kind is called “before middleware”.
Second kind is called “after middleware”.

Before middleware

Lets take a look at handle method composition.

After middleware

Lets take a look at handle method composition.

Global Middleware

If you want middleware to run during each request, register it in: app/Http/Kernel.php in protected property $middleware like this:

Above code fires on every request, to show maintenance page, in case system is in maintenance mode.

Route Middleware
shortcut method

Register middleware in app/Http/Kernel.php in protected property $routeMiddleware like this:

Then you can call it by chaining one, or more to route directly, using registered shortcut keyword, like this:

single keyword:

multiple keywords:

direct method

Instead of using shortcut, you can use middleware class directly:

In this case no need to register anything in Kernel.

Middleware Groups

If you can/need to use some multiple middleware classes repetitively, you can group them.

To group middleware, register new group under selected keyword in protected property $middlewareGroups, in app/Http/Kernel.php, like this:

Now, you can apply all these middleware classes wholesale, by using single keyword ‘web’, they are assigned to.

You can also use many groups, or mix them with single route middleware:

Middleware Parameters

You can pass parameters to middleware handle() method.

… and then use itl like this:

Param ‘editor’ will be captured my handle variable $role and can be used inside.

If you have multiple params, you want to send – use commas, e.g.

And you are using it like this:

Params: editor,another,another2 will be captured by handle() variables: $role, $another, $another2 for use inside of handle().