Registering HttpKernel Middleware

Problem

You want to modify some aspect of the request or response at a low level.

You know you could use before filters or after filters, but the function you want to perform is global across your entire application and it's at a low level.

Solution

Use the App::middleware() method.

First you must register your middleware. We'll call the class MyApp\Middleware in this example.

App::middleware('MyApp\Middleware');

Then you must have the class MyApp\Middleware in your application to handle things.

If your class takes additional construction arguments, you can specify those arguments when you register the middleware.

// 1 arg
App::middleware('MyApp\Middleware', array($arg1));

// 2+ args
App::middleware('MyApp\Middleware', array($arg1, $arg2));

Discussion

It's important to know where the middleware processing occurs. See Understanding the Request Lifecycle.

Here are some other recipes for middleware:

comments powered by Disqus