How to get current route in Symfony 2?
I think this is the easiest way to do this:
class MyController extends Controller
{
public function myAction($_route)
{
var_dump($_route);
}
.....
With Twig : {{ app.request.attributes.get('_route') }}
From something that is ContainerAware (like a controller):
$request = $this->container->get('request');
$routeName = $request->get('_route');
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) { ... }