In a lot of old projects of Symfony 1.4, you will probably find some inconsistent design patterns when it comes to the routing stuff. For example, this used to be on every project i used to work with:
# apps/frontend/config/app.yml
# generic rules
# please, remove them by adding more specific rules
default_index:
url: /:module
param: { action: index }
default:
url: /:module/:action/*
What this basically does is that it will recognize URL's on the project by simply adding the name of the module and the action, for example /articles/read
. So you could easily create a route on the templates with url_for
like this:
<a href="<?= echo url_for("module/action");?>">Go to Module</a>
So theorically, there are no routes registered on the system if you want to generate the routes as text in controllers:
echo $this->generateUrl("module/action");
The previous code will fail for the mentioned reason. Of course, the solution in this case, considering that your project may have hundreds of routes that work with the mentioned pattern, isn't to generate a new route in the yaml file for what you need. Instead, you may simply rely on the good old trick of using the url_for
method in the controller.
Importing the url_for method on the controller
In order to use the url_for
method on the controllers, you will need to load the helper methods like this:
sfApplicationConfiguration::getActive()->loadHelpers(array('Url'));
// Then, you can use the method
echo url_for("module/action");
This is the correct approach as mentioned on this article, retrieving the context instance of symfony 1.4 is a bad practice.
Obtaining helper from the sfContext instance
If you love to break the rules and the above mentioned method isn't working for you, you may simply achieve the same result using the following instruction instead:
sfContext::getInstance()->getConfiguration()->loadHelpers(array('Url'));
// Then, use the url_for method
echo url_for("module/action");
Usually, you should try to avoid obtaining directly the instance from the sfContext as many of the symfony's classes store already a reference to the context, so if you need it, use that one.
Happy coding ❤️!