How to get current route in Symfony 2?
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.
Related videos on Youtube
Comments
-
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 over 12 yearsSolved it myself. In a view:
$view['request']->getParameter('_route');
-
Martin Schuhfuß almost 12 yearsthis is because you are using
{% render... %}
calls withstandalone=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 almost 12 yearsThank you! Am using
<body class="{{ app.request.get('_route') | replace({'_' : '-'}) }}">
for applying page-specific css :) -
Charlie over 11 yearsCan you add more explanation or show sample output to clarify how this solves the problem?
-
wdev over 11 years@charlie symfony.com/doc/master/book/…
-
supernova over 11 years@Charlie It's a predefined variable which gives you the matched route "name"
-
NullPoiиteя over 11 years@got a switchwation for you check meta.stackexchange.com/questions/155258/…
-
luiges90 over 11 yearsgithub.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 over 11 yearsgithub.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 over 11 yearsgithub.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 about 11 yearsThis 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 about 11 yearsI 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 almost 11 yearsA pity is that this works only in the original Action, for any other function it has to be forwarded.
-
jzimmerman2011 over 9 yearsThe 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 almost 8 years@tuxedo25 Think about using RequestStack: symfony.com/blog/new-in-symfony-2-4-the-request-stack
-
ricko zoe over 7 yearsIt 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 over 7 years$request->get('_route'); is slow ! $request->attributes->get('_route'); is better if you do not need the flexibility
-
Charaf about 7 yearsOk, you basically suggest to add extra information to every routes in this files instead of getting the route name ? …
-
Tom Tom about 7 yearsYep 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 about 7 yearsSo… how does your solution not work for all uses-cases exactly?
-
Thomas Bredillet over 6 yearsMore information here : roadtodev.com/comment-recuperer-la-route-actuelle-avec-symfony
-
Nico Haase over 5 yearsThis will not return the name of the handled route
-
Aderemi Dayo about 4 years@NicoHaase It is not rocket science, you are already having the request object
-
sneaky almost 4 years@ThomasBredillet : The link is down.
-
Paweł Napierała over 3 yearsSince Symfony 4.3 this is officially documented method to get current route name: symfony.com/doc/4.3/…
-
Xavi Montero over 3 yearsThe 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/…