Symfony genUrl method

Symfony genUrl is one of the most used methods on symfony 1 series (I’m not sure if it is called the same way in Symfony 2). But what is special about this method? Well, it is the one which generates urls for the whole framework. It is the man when you want to generate urls where to link to. That is its main purpose. Indeed, a lot of methods you usually used called genUrl behind the scenes for example: redirect method, link_to helper, url_for helper.

But, where is located this method? It is located in a usually hidden class, sfWebController. This class provides web specific methods to its parent, sfController, in this case url redirection.

Now let’s see how to use this method:

public function executeShow(sfWebRequest $request)
  $url = $this->getController()->genUrl('articles/deadlock_in_real_life');

$url will contain:


To get that result you should have a route to match in routings.yml:

  url:             /article/:slug
  param:           { module: Post, action: show }
  requirements:    { slug: \w+ }

This way the method knows where to find this pattern. It will replace :slug for deadlock_in_real_life. Another condition is deadlock_in_real_life should match the requirement for the route, in this case the regular expression \w+ (a-z 0-9 _).

Even though this is the easiest way to use genUrl it is not the recommended one because of the overhead. The method will have to parse route by route to find the one which matches with the parameter, in this case articles/deadlock_in_real_life.

It is much better, for the sake of performance, give the route name and the parameters to the genUrl method. Let see this with an example:

public function executeShow(sfWebRequest $request)
  $url = $this->getController()->genUrl('@post_show?slug=deadlock_in_real_life');

$url will contain the same url as before, but this time there was no parsing. Now, the genUrl method with the route name locates and evaluates the pattern directly. Imagine you have a routings.yml file with more than 100 routes. Obviously to parse route by route will be much slower than to locate directly just one.

The absolute parameter

The second parameter for the genUrl method is the one which indicates whether or not the final url will be absolute. By default this parameter has the value of false, that’s why in the previous example we did not get an absolute url. Let’s see how to use this parameter:

$url = $this->getController()->genUrl('@post_show?slug=deadlock_in_real_life', true);

$url will contain:

The final result is the absolute url.

The genUrl method still has more ways to use it. We just describe here the ones you need to understand how it works. Now if more power is what you want, maybe you can take a look at its internals in sfWebController class. Finally here is the url for the sfWebController class API and the url for the Links and the Routing System Chapter in the Symfony Book.

Leave a Reply

Your email address will not be published. Required fields are marked *