Slim 3 gets the current route in middleware

I want to get the name of the current route I'm in the middleware class. Earlier (in Slim 2. *) you could find the current route as follows:

$route = $this->app->router->getCurrentRoute();

But this feature has been removed in version 3.0 Slim. I found the following code in the __invoke Slim\App method:

  // Get the route info $routeInfo = $request->getAttribute('routeInfo'); /** @var \Slim\Interfaces\RouterInterface $router */ $router = $this->container->get('router'); // If router hasn't been dispatched or the URI changed then dispatch if (null === $routeInfo || ($routeInfo['request'] !== [$request->getMethod(), (string) $request->getUri()])) { $request = $this->dispatchRouterAndPrepareRoute($request, $router); $routeInfo = $request->getAttribute('routeInfo'); } 

This means that the current route is stored as the routeInfo attribute in Request . But it seems that my custom middleware class is called before the attribute is set (using the $this->dispatchRouterAndPrepareRoute($request, $router); method). Since calling $request->getAttribute('routeInfo') resolves to NULL .

So my question is; How can I get the current route (or route name) from a middleware function / class?

Or do I just need to copy the code snippet above from Slim\App ?

+7
php slim slim-3
source share
5 answers

For Slim3, an example is shown showing how to get routing information from middleware, which is actually a combination of the previous answers.

 <?php $slimSettings = array('determineRouteBeforeAppMiddleware' => true); // This is not necessary for this answer, but very useful if (ENVIRONMENT == "dev") { $slimSettings['displayErrorDetails'] = true; } $slimConfig = array('settings' => $slimSettings); $app = new \Slim\App($slimConfig); $myMiddleware = function ($request, $response, $next) { $route = $request->getAttribute('route'); $routeName = $route->getName(); $groups = $route->getGroups(); $methods = $route->getMethods(); $arguments = $route->getArguments(); print "Route Info: " . print_r($route, true); print "Route Name: " . print_r($routeName, true); print "Route Groups: " . print_r($groups, true); print "Route Methods: " . print_r($methods, true); print "Route Arguments: " . print_r($arguments, true); }; // Define app routes $app->add($myMiddleware); $app->get('/', function (\Slim\Http\Request $request, Slim\Http\Response $response, $args) { # put some code here.... }) 

In my case, I wanted to add middleware that would allow the user to enter certain routes and redirect to the login page if they were not. I found that the easiest way to do this is to use ->setName() on such routes:

 $app->get('/', function (\Slim\Http\Request $request, Slim\Http\Response $response, $args) { return $response->withRedirect('/home'); })->setName('index'); 

Then, if this route was matched, $routeName in the middleware example would be "index" . Then I determined a list of route arrays that did not require authentication, and checked if the current route was on this list. For example.

 if (!in_array($routeName, $publicRoutesArray)) { # @TODO - check user logged in and redirect if not. } 
+8
source share

Apparently, you can configure Slim to determine the route before moving on to middleware using this option:

 $app = new Slim\App([ 'settings' => [ 'determineRouteBeforeAppMiddleware' => true, ] ]); 

I'm not sure what effect this has, but it works for me :)

+5
source share
 $request->getUri()->getPath() 

Get the current route, even in middleware .

+3
source share

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

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

If you also need a request bit, you will need to do the same manually with dispatchRouterAndPrepareRoute .

 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 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.

+1
source share

Here's how you get the current route in your middleware in Slim framework 3:

 $routeInfo = $request->getAttribute('routeInfo'); 

Note that you must use this function inside __invoke() in your middleware. Here the selection is used:

 public function __invoke($request, $response, $next) { .... $routeInfo = $request->getAttribute('routeInfo'); .... } 

$ routeInfo should contain an object, for example:

 { "0": 1, "1": "route6", "2": { "name": "loremipsum" }, "request": [ "POST", "http://example.org/loremipsum" ] } 
0
source share

All Articles