Symfony routing with and without trailing slashes

UPDATE: Symfony2 version: The Cookbook Routing Redirect URLs with a Trailing Slash.

Symfony routing, by default, seem not work with URLs with trailing slashes.

For example www.symfony-project.org/community works ok, but www.symfony-project.org/community/ results in error 404.

You can fix it easily at application level by overriding sfPatternRouting.

class myPatternRouting extends sfPatternRouting 
{
  public function parse($url) 
  {
    $url = rtrim($url, '/'); # trim trailing slashes before actual routing
    return parent::parse($url);
  }  
}

Put this code in lib directory in myPatternRouting.class.php file and don’t forget to adjust your settings in factories.yml.

all:
  routing:
    class: myPatternRouting
    param:
      generate_shortest_url: true
      extra_parameters_as_query_string: true

That’s it.

  1. by sobstel • May 2010 • blog archive