Generating url using routing from anywhere in Symfony


This post describes how to generate URL from anywhere in a Symfony application. For example, I needed to create URL for forgotten password within an error message for invalid e-mail address. Needless to say, this should be done in the user registration form class.

URL can be generated from anywhere in a Symfony application by using sfContext like in the following example:

$some_url = sfContext::getInstance()->getRouting()->generate('route_name');
📄 View source (.md)