我遇到问题在中间件中获取组路由参数这就是我在做什么
我正在使用[ PHP - SLIM 3 Framework ]
route.php
$app->group('/{lang}', function() use ($container){
//routes ... (ignore the other routes)
})->add(new Middleware($container));
Middleware.php
//middleware.php
class Middleware {
public function __invoke($req, $res, $next)
{
//here is the part which is confusing about how can i get
// {lang} parameter
$req->getAttribute('route')
}
}
答案 0 :(得分:3)
您可以使用getArguments()
- 方法
public function __invoke($req, $res, $next)
{
$route = $req->getAttribute('route');
$args = $route->getArguments();
$lang = $args['lang'];
return $res;
}
注意:您还需要将细长设置determineRouteBeforeAppMiddleware
设置为true。否则,参数不会在中间件中设置。
$container = [
'settings' => [
'determineRouteBeforeAppMiddleware' => true
]
]
$app = new \Slim\App($container);