Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kamleshcgtechno's avatar

How to get current route information in middleware with Lumen 8.x?

Hi, I am using Lumen 8.x version. I need to have the current found controller and action in a middleware, so that I can do some authentication. But I found it impossible, because the pipe is like Middleware1 -> Middleware2-> do the dispatching -> controller@action() -> Middleware2 -> Middleware1.

Therefore before the dispatching, I cannot get the route info. It is definitely not right to do it after the $controller->action().

I did some research and found this.

$allRoutes = $this->app->getRoutes(); $method = \Request::getMethod(); $pathInfo = \Request::getPathInfo(); $currentRoute = $allRoutes[$method.$pathInfo]['action']['uses'];

But this does not work for me when visiting URI like app/role/1, because $allRoutes only have index of app/role/{id} instead of app/role/1.

Kindly suggest how can we do this? Thanks a lot.

0 likes
2 replies
bugsysha's avatar

If you explain the reason why you need that kind of logic, I'm sure someone will give better suggestion how to implement it.

Please or to participate in this conversation.