I know how to add multiple middleware class in $app
:
$app->add( new Middlewares\VerifyCsrfToken() );
$app->add( new Middlewares\RemoveTrailingSlash() );
But how on route?
$app->get('/', function($request, $response){
return $response;
})->add( new Middlewares\VerifyCsrfToken() )
->add( new Middlewares\RemoveTrailingSlash() );
As noted in the comments, add()
returns an instance of the Route
object, so you can simply chain multiple add()
s together.
This unit test shows how to do it:
$app = new App();
$app->get('/', function ($req, $res) {
return $res->write('Center');
})->add(function ($req, $res, $next) {
$res->write('In1');
$res = $next($req, $res);
$res->write('Out1');
return $res;
})->add(function ($req, $res, $next) {
$res->write('In2');
$res = $next($req, $res);
$res->write('Out2');
return $res;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With