Slim 3 get current route in middleware

后端 未结 5 1929
故里飘歌
故里飘歌 2021-01-04 13:30

I want to get the name of the current I route in a middleware class. Previously (in Slim 2.*) you could fetch the current route like so:

$route = $this->ap

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-04 14:06

    Does the following provide you with sufficient information you require or do you also need the 'request' bit in routeInfo?

    $app->getContainer()->get('router')->dispatch($req);
    

    If you also require the 'request' bit then you will need to manually do the same thing dispatchRouterAndPrepareRoute does.

    if ($routeInfo[0] === Dispatcher::FOUND) {
                $routeArguments = [];
                foreach ($routeInfo[2] as $k => $v) {
                    $routeArguments[$k] = urldecode($v);
                }
    
                $route = $router->lookupRoute($routeInfo[1]);
                $route->prepare($request, $routeArguments);
    
                // add route to the request's attributes in case a middleware or handler needs access to the route
                $request = $request->withAttribute('route', $route);
            }
    
            $routeInfo['request'] = [$request->getMethod(), (string) $request->getUri()];
    

    Hope this helps.

提交回复
热议问题