Understanding What Middleware Is

Problem

You've heard about Laravel's middleware, but don't understand it.

Solution

It's pretty easy really.

Each middleware class is constructed with a reference to the middleware below it and must implement the handle() method. This creates a stack of middleware.

+---------------------+
| Your middleware     |
+---------------------+
| Session middleware  |
+---------------------+
| Cookie middleware   |
+---------------------+
| Laravel application |
+---------------------+

The handle() method your middleware implements will receive the Request. Your handle() implementation must do four things:

  1. Modify the Request if needed.
  2. Call the middleware below it, receiving the Response.
  3. Modify the Response if needed.
  4. Returns the Response.

The whole process operates a bit like Russian Nested Dolls. The Laravel application is that tiny, innermost doll.

When the Laravel application's handle() method is called, it will boot the application, dispatch the request and return the response.

Discussion

Don't forget you can also shutdown your middleware.

If your middleware class implements TerminableInterface then when the application shuts down your middleware's terminate() method will be called.

comments powered by Disqus