How to get name of currently executing route


#1

Does anybody know how to get the name of the route which is being executed/handled in the current request?

Thanks


#2

The \Slim\Route object has a getName() method. I recall you might need 'determineRouteBeforeAppMiddleware' => true in your settings, depending on when you need it. You should be able to:

$request->getAttribute('route')->getName();

#3

Thank you. But allow me another related question:

How can I retrieve a properly initialized Request object from the global context? I’m asking because I did

$container->get('request');

which does return a Request object, but it doesn’t have the state/attributes which the Request passed to the controller functions has. I’ve looked through the Slim code but so far have not found anything which seems to do what I want.

Basically, I am interested in the route name, but in order to get this, I need a properly initialized Request object.

Thanks.


#4

The Request object is immutable, so the one in the container isn’t the one you want. You need to use the one passed to the middleware, controller/action, or get the value you need from one of those places and pass it to where you do need it.


#5

Thanks, that what I figured (and was afraid of). Need to re-engineer some code :slight_smile:

Thanks