21.10.2015 Views

1-33

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Symfony2 – Franz Jordán 2011<br />

How to allow a "/" character in a<br />

route parameter<br />

Sometimes, you need to compose URLs with parameters that can contain a slash /. For example,<br />

take the classic /hello/{name} route. By default, /hello/Fabien will match this route but<br />

not/hello/Fabien/Kris. This is because Symfony uses this character as separator between<br />

route parts.<br />

This guide covers how you can modify a route so that /hello/Fabien/Kris matches<br />

the/hello/{name} route, where {name} equals Fabien/Kris.<br />

Configure the Route<br />

By default, the symfony routing components requires that the parameters match the following<br />

regex pattern: [^/]+. This means that all characters are allowed except /.<br />

You must explicitly allow / to be part of your parameter by specifying a more permissive regex<br />

pattern.<br />

_hello:<br />

pattern: /hello/{name}<br />

defaults: { _controller: AcmeDemoBundle:Demo:hello }<br />

requirements:<br />

name: ".+"<br />

That's it! Now, the {name} parameter can contain the / character.<br />

67

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!