slimphp / Slim-Http

A set of PSR-7 object decorators providing useful convenience methods

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Accessing Route Arguments In middleware

nixoncode opened this issue · comments

An Invokable class middleware has the following signature

__invoke(ServerRequest $request, RequestHandlerInterface $handler)

is there an easier way of accessing the route parameters? Right now I have the below code

// returns Slim\Routing\RoutingResults obj
$routingResult = $request->getAttribute('__routingResults__');
        
$key = $routingResult->getRouteArguments()['key'];

Previously I could do something like $key = $request->getAttribute('key')

Use the RouteContext object first of all.

$routeContext = RouteContext::fromRequest($request);

And the getRouteArguments() method is correct.

I don’t quite understand what you’re saying though about $request->getAttribute(‘key’). That functionality is provided by the PSR-7 implementation and is doing as it should. It has nothing to do with route arguments tho.

Thanks much better.
ignore my last statement about $request->getAttribute(‘key’).

I think this should be mentioned somewhere in the docs