是否可以为slim3中的每个路由创建中间件组?我创建了一个路由,在middleware.php
文件中我设置了一个中间件列表,事实证明所有路由都将通过此列表,但我不需要它。第二个问题是如何访问中间件中的属性,输出$request->getAttribute('paramName')
,我得到NULL?
你说你不希望所有路由匹配(我假设这是应用程序范围的中间件的情况),但你没有详细说明你想要什么样的匹配。
所以我不确定一组中间件是什么意思。您可以将中间件添加到特定路由或路由组。
从文档中添加中间件到路由组的示例:
$app->group('/utils', function () use ($app) {
$app->get('/date', function ($request, $response) {
return $response->getBody()->write(date('Y-m-d H:i:s'));
});
$app->get('/time', function ($request, $response) {
return $response->getBody()->write(time());
});
})->add(function ($request, $response, $next) {
$response->getBody()->write('It is now ');
$response = $next($request, $response);
$response->getBody()->write('. Enjoy!');
return $response;
});
https://www.slimframework.com/docs/v3/concepts/middleware.html#group-middleware