Allowing Slashes in a Symfony Route

Symfony routing is a wonderful tool. Recently, however, I have wanted to include a slash in a route parameter. This can seem problematic as symfony uses slashes to delimit parameters. For example, lets say I wanted the url http://www.example.com/user/john/jacob/jingleheimer/schmidt to match a route that looked something like this:

#routing.yml
users:
  url: /user/:name
  param: {module: mymod, action: myaction}

The url http://www.example.com/user/Dave would match but the url http://www.example.com/user/john/jacob/jingleheimer/schmidt would not. Thankfully, this can be easily fixed with the following adjustment.

#routing.yml
users:
  url: /user/:name
  param: {module: mymod, action: myaction}
  requirements:
    name: ".+"

This changes the regular expression used to recognize the parameter name and allows John Jacob Jingleheimer Schmidt to access his page.

Update:
Its worth noting that url_for will automatically encode slashes inside parameters. As a result, if you are using url_for with the scenario above, you may wish to run urldecode on the resulting url.

This entry was posted in 1.4, Symfony and tagged , , , , . Bookmark the permalink.

One Response to Allowing Slashes in a Symfony Route

  1. I demand that you discontinue defaming my likeness in your programming examples!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>