Getting current route name from template in Symfony


When creating webpages with navigation and menus, it is often necessary to assign a css class for the menu item that the visitor is currently viewing. This post shows an example how to get the current route name from a template for that purpose.

Route name of the currently running module/action can be accessed from template like in the following example:

$route = $sf_context->getInstance()->getRouting()->getCurrentRouteName();

Also, current route name can be accessed from anywhere by:

$route = sfContext::getInstance()->getRouting()->getCurrentRouteName();
📄 View source (.md)