Symfony routing with and without trailing slashes

about 2 years ago sobstel php, symfony comments

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.

1 class myPatternRouting extends sfPatternRouting 
2 {
3   public function parse($url) 
4   {
5     $url = rtrim($url, '/'); # trim trailing slashes before actual routing
6     return parent::parse($url);
7   }  
8 }

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.

Comments

almost 2 years ago Dmitriy http://www.infosoft.ua #0

Great! 2 hours of googling, and this is exact what i need:
1. Solved Doctrine problem with slashes at the end
2. Creating url's with slash at the end and then correctly parsing by symfony routing system

Sanks!

P.S. Solutions based on .htaccess file modification doesn't work

over 1 year ago NiKo #1

What about duplicate contents? two urls serving the very same contents and google is grumpy. a 301 redirect should be better, but you'd have to use sfContext::getInstance() to retrieve the controller from the routing class instance: ugly.

5 months ago m_gol #2

Thanks! So much simpler than editing .htaccess...

New comment

  1. (with http or https prefix)

  2. (use [code][/code] tags to post a code snippet)

  3. (javascript is required)

Website content premeditately commited by malczak & sobstel.
Layout by mlando. Icons by dryicons.com. All rights reserved.