获得从小组路线路由参数SLIM 3 PHP

问题描述:

我有问题越来越中间件这里的组路线参数

获得从小组路线路由参数SLIM 3 PHP

我使用[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') 
    } 
} 

您可以用 - 方法

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);