0

I am having issue getting the group route parameter in middleware here's what i am doing

I am using [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')
   }
}
Abdul Rafay
  • 312
  • 6
  • 17

1 Answers1

3

You can do this with the getArguments()-method

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. Otherwise the argument is not set in the middleware.

$container = [
    'settings' => [
        'determineRouteBeforeAppMiddleware' => true
    ]
]
$app = new \Slim\App($container);
jmattheis
  • 10,494
  • 11
  • 46
  • 58