在SLIM 3 PHP中从组路由获取Route参数

时间:2017-03-05 06:56:37

标签: php slim-3

我遇到问题在中间件中获取组路由参数这就是我在做什么

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

1 个答案:

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