[英]Get Route parameter from group route in SLIM 3 PHP
I am having issue getting the group route parameter in middleware here's what i am doing 我在中间件中获取组路由参数时遇到问题,这是我在做什么
I am using [ PHP - SLIM 3 Framework ] 我正在使用[ PHP-SLIM 3 Framework ]
route.php
route.php
$app->group('/{lang}', function() use ($container){
//routes ... (ignore the other routes)
})->add(new Middleware($container));
Middleware.php
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')
}
}
You can do this with the getArguments()
-method 您可以使用
getArguments()
方法来执行此操作
public function __invoke($req, $res, $next)
{
$route = $req->getAttribute('route');
$args = $route->getArguments();
$lang = $args['lang'];
return $res;
}
Note: you also need to set the slim setting determineRouteBeforeAppMiddleware
to true. 注意:您还需要纤薄设置设为
determineRouteBeforeAppMiddleware
为true。 Otherwise the argument is not set in the middleware. 否则,不会在中间件中设置参数。
$container = [
'settings' => [
'determineRouteBeforeAppMiddleware' => true
]
]
$app = new \Slim\App($container);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.