The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Current Route

paul's Avatar

paul

24 Apr, 2012 07:14 AM

Hello,

How is it possible to know the current route name (for example in the middleware) ?

i do :

$app = new Slim();
$app->map('/foo', function(){})->name('foo');
$app->map('/bar', function(){})->name('another_name');

in a middleware i would like to do something like this:

if( $env->getRoute()->getName() == 'foo' )
{
 // ...
}
else
{
}
  1. Support Staff 1 Posted by Andrew Smith on 24 Apr, 2012 10:59 AM

    Andrew Smith's Avatar

    From what I gather there is currently no way of doing this at the moment. I
    will be pushing for this to be added as it would make adding a
    Authentication middleware easier.

  2. 2 Posted by avalak on 24 Apr, 2012 02:18 PM

    avalak's Avatar

    In Middleware

    public function call() {
      $app = $this->app;
      $log = $app->getLog();
      $routes = $app->router()->getMatchedRoutes();
      // all matched routes.
      $names = array_map(function ($route) { return $route->getName(); }, $routes);
      // all _named_ matched routes.
      $names = array_filter($names, function ($name) { return strlen($name); });
    
      if (count($names)) { // if 0 - noname route or notFound
        $log->debug('Matched routes: '. join(', ', $names));
    
        if (in_array('index', $names)) {
          $log->debug('index url');
        }
      }
      $this->next->call();
    }
    

    In Route Middleware

    return function ($req, $res, $route) {
      $params = $route->getParams();
      $route->getName();
    }
    
  3. Support Staff 3 Posted by Josh Lockhart on 26 Apr, 2012 01:14 AM

    Josh Lockhart's Avatar

    I'll be adding an official solution for this into 1.6.3. Follow this issue for updates:

    https://github.com/codeguy/Slim/issues/289

    Err... I mean this issue:

    https://github.com/codeguy/Slim/issues/290

  4. Andrew Smith closed this discussion on 08 Aug, 2012 11:30 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac