user1272589
user1272589

Reputation: 809

Symfony: Get routing name from module / action

In routing.yml

Login:
  url:   /login
  param: { module: access, action: login }

then I have:

$module = 'access';
$action = 'login';

I want to get the routing name ?? // login

Something like getRoutingName($module, $action) return "login".

Upvotes: 0

Views: 2398

Answers (2)

j0k
j0k

Reputation: 22756

What you are looking for is almost getRouteThatMatchesParameters. It find a route for some parameters (module, action & query string).

Here is a quick snippet to retrieve a route name from a module & action name:

$routes = $this->getContext()->getRouting()->getRoutes();
foreach ($routes as $name => $route)
{
  if ($route->matchesParameters(array('module' => 'access', 'action' => 'login'), $this->getContext()))
  {
    var_export($name);
    break;
  }
}

Be careful. I put break because it can retrieve multiple route (for example it will often return the default route (which is generally /:module/:action/*)).

Upvotes: 4

Volodymyr Smotesko
Volodymyr Smotesko

Reputation: 438

$currentRoute = $this->getContext()->getRouting()->getCurrentInternalUri(false);

Upvotes: -3

Related Questions