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?
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?
With Twig :
{{ app.request.attributes.get('_route') }}
With Symfony 3.3, I have used this method and working fine.
I have 4 routes like
And just one line make an active class for all routes.
All I'm getting from that is
_internal
I get the route name from inside a controller with
$this->getRequest()->get('_route').
Even the code tuxedo25 suggested returns_internal
This code is executed in what was called a 'Component' in Symfony 1.X; Not a page's controller but part of a page which needs some logic.
The equivalent code in Symfony 1.X is:
sfContext::getInstance()->getRouting()->getCurrentRouteName();
You can get the route name from the request object from within the controller.
I think this is the easiest way to do this:
From something that is ContainerAware (like a controller):