Middleware
Lumberjack supports PSR-15/7 Middleware. Middleware can be added via the Router to individual routes or route groups or via WordPress Controllers.
Adding Middleware to a route
At it's simplest, adding Middleware to a route can be done by passing an object to the middleware()
function:
Multiple middleware can be added by passing more params to the middleware()
function:
Or alternatively, you can also pass an array of middleware:
Adding Middleware to a group
Middleware can also be added to a group. To do so you need to pass an array as the first parameter of the group()
function instead of a string.
You can also pass an array of middleware if you need more than one:
Defining Middleware on Controllers
You can also apply Middleware on a Controller class too, either for use with the Router or as a WordPress Controller. In order to do this your Controller must extend the App\Http\Controllers\Controller
base class.
Middleware is added by calling the middleware()
function in your Controller's __constructor()
.
By default all Middleware added via a Controller will affect all methods on that class. To limit what methods Middleware applies to you can use only()
and except()
:
Middleware Aliases
Available in v4.3.0
and above
Creating new instances of middleware in your routes or controllers can have a couple of negative outcomes:
The objects are instantiated and take up memory whether they're used or not
Your route definitions can become less readable
These issues can both be addressed using Middleware Aliases. Instead of creating instances in your code, you register a string alias that can be used instead.
Registering a Middleware Alias
To register an alias you can use the MiddlewareAliases
facade. It is recommended that you use this in the boot()
method of the AppServiceProvider
class.
There are 3 ways to define your middleware alias:
A closure factory (recommended - always creates a new instance when used)
A class name (always resolves a new instance from the Container when used)
A previously instantiated object (you don't get the benefits of lazy loading)
See "Setting entries in the container" for more information about the differences between these. While only the class name uses the container, principally they behave in the same way with regards to lazy-loading and object instantiation.
Using a closure factory (recommended)
It is recommended that the alias is registered using a closure that acts as a factory, like so:
Using a class name
Using a previously instantiated object
When using this method, you do not get the benefits of lazy loading. i.e. The object will be created whether or not it is needed for the current request.
Using a Middleware Alias
Middleware Aliases can be used anywhere middleware can normally be used, both in the Router and through Controllers.
If, for example, an Alias had been registered for an AuthMiddleware
with the key auth
like so:
You could use this in your route definition like this:
Middleware Alias Parameters
You can register Middleware Aliases which accept parameters. It is advised if you're doing this to use the Closure Factory registration technique.
In this example, $key
will have the value X-Key
and $value
will have the value of HeaderValue
.
Creating custom Middleware
While you can use any PSR 15/7 Middleware, sometimes you need to write your own. In this example, we will create custom middleware that adds the response header X-Foo
.
First, create the class in app/Http/Middleware/ExampleMiddleware.php
(create the folder if it doesn't exist) with the following content:
In our example, we want to modify the response. Below we add the X-Foo
header to the response:
If you need to modify the request before it gets passed to your application, you can do so before $handler->handle($request)
gets called:
Last updated