Friday, May 7, 2010

How to allow trailing slash in urls in Symfony (1.4)

Symfony's routing system is made so it doesn't allow trailing slashes. It's really a problem, because it can't be solved through just some rewrite rule in .htaccess, because the request path info is not coming from .htaccess (but from the $_SYSTEM array).

Of course there is an easy solution. Symfony's factory architecture makes it easy to extend or modify built-in functionality. All you have to do is to change the routing class in factories.yml and prepare that class:

/apps/frontend/config/factories.yml:
all:
routing:
# class: sfPatternRouting
class: myPatternRouting

Create the class:
apps/frontend/lib/myPatternRouting.class.php
<?php
class myPatternRouting extends sfPatternRouting
{
protected function normalizeUrl($url)
{
$url = parent::normalizeUrl($url);

// remove trailing slash
$url = preg_replace('/\/$/', '', $url);

return $url;
}
}

Okey, this is it.

But maybe it's a better way to catch the flow in an earlier point. Because this way, though routing will parse urls with trailing slashes fine, but calls to $request->getPathInfo() will still give the urls with the trailing slashes. It can has some unwanted side effects. It's better if the whole framework sees the request url as if it had no trailing slash.

What to do for that? Change the request class instead of the routing.

/apps/frontend/config/factories.yml:
all:
routing:
class: sfPatternRouting

request:
class: myWebRequest

apps/frontend/lib/myWebRequest.class.php
<?php
class myWebRequest extends sfWebRequest
{
public function getPathInfo()
{
$pathInfo = parent::getPathInfo();

// cut off trailing slash
$pathInfo = preg_replace('/\/$/', '', $pathInfo);

return $pathInfo;
}
}

40 comments:

  1. or just edit .htaccess

    #to avoid trailing slash problem
    RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

    ReplyDelete
  2. Thanks that's fine also, though i don't really like external redirects.

    ReplyDelete
  3. But can we make it a pluggin ?

    ReplyDelete
  4. The regex that you are using, ^(.*)/$ matches requests that end with a slash, so example.com/foo will not match it. To get more info on this theme please visit site of my company.

    ReplyDelete
  5. Tyhe blog is great! By the way, my company has a great website too. Visit it.

    ReplyDelete
  6. Good post. I found a lot of useful information. By the way, my company provides professional writing service!

    ReplyDelete
  7. Thanks for help, you can visit also my company which helps with essay papers

    ReplyDelete
  8. I found this post really helpful for me. But I can also advice you express-essays.com.

    ReplyDelete
  9. Thanks for help! Experience makes high quality and distinguished custom essay writing companies can write the term papers of superb quality. That's a proper movement to success.

    ReplyDelete
  10. Interesting post. By the way, students do not have to worry anymore about writing essays. Using first-rate custom essay writing service, that are available 24/7, learners will always get professionally written essays.

    ReplyDelete
  11. Really well created work you have post here Thanks a lot.

    ReplyDelete
  12. The learning is done in making of assignments and essay writing. The students learn lots of things and abilities are enhanced. The student becomes more skillful and able person. He can undertake different tasks of education very easily by hiring professional writers in budgeted price.

    ReplyDelete
  13. If you don’t have free time or you tired after studying, the team of professionals write good essays for you!Save your time for your friends!

    ReplyDelete
  14. Essay writing is not a guileless task as some put it. It always gives some people, time perdition. There are numerous solutions that have been put across by the custom term papers

    ReplyDelete
  15. Best-essays-writers.org is the Best essay writing service where you can buy essays of all types at a cheap price.They have a team of professionals who can do it on time and the most important for low price best-essays-writers.org

    ReplyDelete
  16. I really loved reading your blog. It was very well authored and easy to understand.

    ReplyDelete
  17. Great article about how to allow trailing slash in urls in Symfony. My brother would like this article! And I like cooking! Especially I like to cook cakes for my family! And when I need to write essays, I remembered about essay services, where I can buy customized essays .

    ReplyDelete
  18. Great post full of useful tips! My site is fairly good and I am also having a hard time getting my readers to leave comments. Analytics shows they are coming to the site but I have a feeling “nobody wants to be first”

    # Writer @ online cheap essay writing service

    ReplyDelete
  19. The point when students and other learners are given the assignment of inquiring about or making presentation then they looked busy in arranging the assignments on time so that they can get maximum marks. This is conceivable just in case they have guidelines. That is why I recommend the best http://custom-writing.net

    ReplyDelete
  20. Thanks a lot!As for me,it's very useful information how to allow trailing slash in urls in symfony.) I am always searching online for articles that can help me.Keep working, great job.www.advanced-writer.com and please welcome to my website

    ReplyDelete
  21. I've always respected programmers, because do not understand this)) And for those who don't understand how you can write high-quality essays, these profesionals essay-mania.com will help you for sure!

    ReplyDelete
  22. I'm expressed by the article "How to allow trailing slash in urls in Symfony", it is one of the best ones on this blog. View more articles and essays at http://customwriting-s.com/ right now!

    ReplyDelete
  23. It's very good that Symfony's factory architecture makes it easy to extend or modify built-in functionality because it's real problem that system doesn't allow trailing dashes! Thanks for sharing valuable info! Keep on writing with the best online writing service papers-writings.com

    ReplyDelete
  24. Thank you for sharing, you help a lot. One more source that can help students to cope with their studying troubles is academic-writings.com. Enter it and obtain excellent academic papers.

    ReplyDelete
  25. Thanks that you share with us this amazing post! Also want share with your link on this site http://essay-professors.com , i think it will be very helpful for you when you want order your college essay!

    ReplyDelete