Search code examples
phpslim

Get arguments using Request instance in Slim php


I am trying to get the $args from $request, is there any way to do it, if so how, here is a sample of what i am doing:

class JwtMiddleware
{

    public function __construct($settings)
    {

    }

    public function __invoke(Request $request, RequestHandler $handler)
    {
        $route = $request->getAttribute('route');
        $id = $route->getArgument('user_id');
        // this doesn't work

        /*
         //
         code 
         //
        */

    }
}

thank you in advance


Solution

  • To get the route arguments from the request, you can do the following:

    use Slim\Routing\RouteContext;
    // ...
    
    $routeContext = RouteContext::fromRequest($request);
    $route = $routeContext->getRoute();
            
    $userId = $route->getArgument('user_id');
    
    // ...