Search code examples
phpsymfonyinternationalizationbundle

Symfony 2 - How can I get current route name using bundle JMSI18nRoutingBundle


I'm using JMSI18nRoutingBundle in a Symfony 2 project to translate my routes.

I'm having a problem on how to obtain the current route name (both in the Twig's template and in the Symfony controller)...

I tried:

In the Twig's template:

{{ app.request.attributes.get('_route') }}
{{ app.request.get('_route') }}

and in the controller:

$this->container->get('request')->get('_route')
$this->container->get('request')->attributes->get('_route')

But none worked out... I think it's because I'm using JMSI18nRoutingBundle...

Can someone help me?


Solution

  • It's ok, I found the solution :

    $actual_link = preg_replace('/\/app_dev.php/', '', $_SERVER['REQUEST_URI']);
    $route = $this->get('router')->match($actual_link)['_route'];
    

    First line : "app_dev.php" has to be removed if we are in dev mode.
    Second line : Match current URL with routing to find the current route name.