How to get current route in Symfony 2?

212,721

Solution 1

From something that is ContainerAware (like a controller):

$request = $this->container->get('request');
$routeName = $request->get('_route');

Solution 2

With Twig : {{ app.request.attributes.get('_route') }}

Solution 3

I think this is the easiest way to do this:

class MyController extends Controller
{
    public function myAction($_route)
    {
        var_dump($_route);
    }

    .....

Solution 4

Symfony 2.0-2.1
Use this:

    $router = $this->get("router");
    $route = $router->match($this->getRequest()->getPathInfo());
    var_dump($route['_route']);

That one will not give you _internal.

Update for Symfony 2.2+: This is not working starting Symfony 2.2+. I opened a bug and the answer was "by design". If you wish to get the route in a sub-action, you must pass it in as an argument

{{ render(controller('YourBundle:Menu:menu', { '_locale': app.request.locale, 'route': app.request.attributes.get('_route') } )) }}

And your controller:

public function menuAction($route) { ... }

Solution 5

There is no solution that works for all use cases. If you use the $request->get('_route') method, or its variants, it will return '_internal' for cases where forwarding took place.

If you need a solution that works even with forwarding, you have to use the new RequestStack service, that arrived in 2.4, but this will break ESI support:

$requestStack = $container->get('request_stack');
$masterRequest = $requestStack->getMasterRequest(); // this is the call that breaks ESI
if ($masterRequest) {
    echo $masterRequest->attributes->get('_route');
}

You can make a twig extension out of this if you need it in templates.

Share:
212,721

Related videos on Youtube

IlyaDoroshin
Author by

IlyaDoroshin

Frontend developer. React+Redux.

Updated on May 20, 2020

Comments

  • IlyaDoroshin
    IlyaDoroshin almost 4 years

    How do I get the current route in Symfony 2?

    For example, routing.yml:

    somePage:
       pattern: /page/
       defaults: { _controller: "AcmeBundle:Test:index" }
    

    How can I get this somePage value?

  • alexismorin
    alexismorin over 12 years
    Solved it myself. In a view: $view['request']->getParameter('_route');
  • Martin Schuhfuß
    Martin Schuhfuß almost 12 years
    this is because you are using {% render... %} calls with standalone=true. With caching (AppCache.php or varnish with ESI) enabled this will cause the standalone views to be requested with a seperate HTTP-Request (this is where the route _internal comes into play) in order ro be independently cacheable.
  • Salman von Abbas
    Salman von Abbas almost 12 years
    Thank you! Am using <body class="{{ app.request.get('_route') | replace({'_' : '-'}) }}"> for applying page-specific css :)
  • Charlie
    Charlie over 11 years
    Can you add more explanation or show sample output to clarify how this solves the problem?
  • wdev
    wdev over 11 years
  • supernova
    supernova over 11 years
    @Charlie It's a predefined variable which gives you the matched route "name"
  • NullPoiиteя
    NullPoiиteя over 11 years
    @got a switchwation for you check meta.stackexchange.com/questions/155258/…
  • luiges90
    luiges90 over 11 years
    github.com/symfony/symfony/issues/854 request.attributes.get('_route') is not reliable because it is for debug purpose only (symfony dev said) and does not work if request is forwarded... see supernova's answer which are documented and are more fail-safe
  • luiges90
    luiges90 over 11 years
    github.com/symfony/symfony/issues/854 request.attributes.get('_route') is not reliable because it is for debug purpose only (symfony dev said) and does not work if request is forwarded... see supernova's answer which are documented and are more fail-safe
  • luiges90
    luiges90 over 11 years
    github.com/symfony/symfony/issues/854 I am not sure about this, $route['_route'] seems problematic but might not be symfony dev talks about. The cookbook does not mention about _route of $router->match() output ..
  • netmikey
    netmikey about 11 years
    This is definitely the best answer to the original question. As a side note: it does not work, however, with sub-requests like {% render "SomeBundle:SomeController:someAction" %}, where you'll get the value '_internal' again.
  • netmikey
    netmikey about 11 years
    I fully agree with @luiges90. The PHPDoc of $router->match() says "@return array An array of parameters" which seems very internal. I don't want to rely on it, but right now, it seems to be the only viable solution when dealing with sub-requests.
  • Darek Wędrychowski
    Darek Wędrychowski almost 11 years
    A pity is that this works only in the original Action, for any other function it has to be forwarded.
  • jzimmerman2011
    jzimmerman2011 over 9 years
    The reason for this not working when something is forwarded is due to the fact that you forward directly to a controller, not a route. As such, Symfony doesn't know what route that is for. Typically, you have one route to one controller, so it may seem weird that this can't report anything besides "_internal", however, it is possible to create general-purpose controllers that get associated with more than one route definition. When you consider all of this, I think this "gotcha" makes more sense.
  • mYkon
    mYkon almost 8 years
    @tuxedo25 Think about using RequestStack: symfony.com/blog/new-in-symfony-2-4-the-request-stack
  • ricko zoe
    ricko zoe over 7 years
    It is not recommended to use $request->get() directly because it's slow: github.com/symfony/http-foundation/blob/2.8/Request.php#L712
  • Tom Tom
    Tom Tom over 7 years
    $request->get('_route'); is slow ! $request->attributes->get('_route'); is better if you do not need the flexibility
  • Charaf
    Charaf about 7 years
    Ok, you basically suggest to add extra information to every routes in this files instead of getting the route name ? …
  • Tom Tom
    Tom Tom about 7 years
    Yep especially if you need to be able to call the controller itself later down the line (forwards, partial rendering, etc...) passing the name as a parameter is the only way here because you are not calling a route at all in that case. As for _route being meant for debug purposes don't take it out on me ^^'
  • greg0ire
    greg0ire about 7 years
    So… how does your solution not work for all uses-cases exactly?
  • Thomas Bredillet
    Thomas Bredillet over 6 years
  • Nico Haase
    Nico Haase over 5 years
    This will not return the name of the handled route
  • Aderemi Dayo
    Aderemi Dayo about 4 years
    @NicoHaase It is not rocket science, you are already having the request object
  • sneaky
    sneaky almost 4 years
    @ThomasBredillet : The link is down.
  • Paweł Napierała
    Paweł Napierała over 3 years
    Since Symfony 4.3 this is officially documented method to get current route name: symfony.com/doc/4.3/…
  • Xavi Montero
    Xavi Montero over 3 years
    The doc link is broken. Nevertheless, the corresponding page for the symfony 5.3 version actually states that getting the _route is the way to go, actually: symfony.com/doc/5.3/…