Skip to content

Middleware

Joshua Parker edited this page Dec 23, 2020 · 3 revisions

PSR-7/15 Middleware can be added to both routes and groups.

Adding Middleware to a route

At it's simplest, adding Middleware to a route can be done by passing an object to the middleware() method:

$middleware = new AddHeaderMiddleware('X-Key1', 'abc');

$router->get('hello-world', '\Mvc\App\MyControllers\HelloWorldController@sayHello')->middleware($middleware);

Multiple middleware can be added by passing more parameters to the middleware() method:

$header = new AddHeaderMiddleware('X-Key1', 'abc');
$auth = new AuthMiddleware();

$router->get('auth', '\Mvc\App\MyControllers\TestController@testMethod')->middleware($header, $auth);

Or alternatively, you can also pass an array of middleware:

$header = new AddHeaderMiddleware('X-Key1', 'abc');
$auth = new AuthMiddleware();

$router->get('auth', '\Mvc\App\MyControllers\TestController@testMethod')->middleware([$header, $auth]);

Adding Middleware to all routes

If you would like to add a middleware that is going to affect all routes, then use the setBaseMiddleware method.

$router->setBaseMiddleware([
    new AddHeaderMiddleware('X-Key', 'abc'),
]);

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.

$header = new AddHeaderMiddleware('X-Key1', 'abc');

$router->group(['prefix' => 'my-prefix', 'middleware' => $header]), function ($group) {
    $group->map(['GET'], 'route1', function () {}); // `/my-prefix/route1`
    $group->map(['GET'], 'route2', function () {}); // `/my-prefix/route2`
});

You can also pass an array of middleware if you need more than one:

$header = new AddHeaderMiddleware('X-Key1', 'abc');
$auth = new AuthMiddleware();

$router->group(['prefix' => 'my-prefix', 'middleware' => [$header, $auth]]), function ($group) {
    $group->map(['GET'], 'route1', function () {}); // `/my-prefix/route1`
    $group->map(['GET'], 'route2', function () {}); // `/my-prefix/route2`
});

Defining Middleware on Controllers

You can also apply Middleware on a Controller class too. In order to do this your Controller must extend the Qubus\Routing\Controller\BaseController abstract class.

Middleware is added by calling the middleware() function in your Controller's __constructor().

use Mvc\App\MyControllers;

class MiddlewareController extends BaseController
{
    public function __construct()
    {
        // Add one at a time
        $this->middleware(new AddHeaderMiddleware('X-Key1', 'abc'));
        $this->middleware(new AuthMiddleware());

        // Add multiple with one method call
        $this->middleware([
            new AddHeaderMiddleware('X-Key1', 'abc'),
            new AuthMiddleware(),
        ]);
    }
}

By default all Middlewares added via a Controller will affect all methods on that class. To limit what methods a Middleware should be applied to, you can use only() and except():

use Mvc\App\MyControllers;

class MiddlewareController extends BaseController
{
    public function __construct()
    {
        // Only apply to `send()` method
        $this->middleware(new AddHeaderMiddleware('X-Key1', 'abc'))->only('send');

        // Apply to all methods except `show()` method
        $this->middleware(new AuthMiddleware())->except('show');

        // Multiple methods can be provided in an array to both methods
        $this->middleware(new AuthMiddleware())->except(['send', 'show']);
    }
}