|
| 1 | +.. index:: |
| 2 | + single: Routing; Allow / in route parameter |
| 3 | + |
| 4 | +How to allow / character in a route parameter |
| 5 | +============================================= |
| 6 | + |
| 7 | +Sometimes, you need to compose URLs with parameters that can contain a slash |
| 8 | +``/``. But Symfony uses this character as separator between route parts. |
| 9 | + |
| 10 | +Configure the route |
| 11 | +------------------- |
| 12 | + |
| 13 | +By default, the symfony routing components requires that the parameters |
| 14 | +match the following regex pattern: ``[^/]+``. This means that all characters |
| 15 | +are allowed excepted ``/``. |
| 16 | + |
| 17 | +You must explicitely allow ``/`` to be part of your parameter specifying |
| 18 | +a more permissive regex pattern. |
| 19 | + |
| 20 | +.. configuration-block:: |
| 21 | + |
| 22 | + .. code-block:: yaml |
| 23 | +
|
| 24 | + _hello: |
| 25 | + pattern: /hello/{name} |
| 26 | + defaults: { _controller: AcmeDemoBundle:Demo:hello } |
| 27 | + requirements: |
| 28 | + name: ".+" |
| 29 | +
|
| 30 | + .. code-block:: xml |
| 31 | +
|
| 32 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 33 | +
|
| 34 | + <routes xmlns="http://symfony.com/schema/routing" |
| 35 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 36 | + xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> |
| 37 | +
|
| 38 | + <route id="_hello" pattern="/hello/{name}"> |
| 39 | + <default key="_controller">AcmeDemoBundle:Demo:hello</default> |
| 40 | + <requirement key="name">.+</requirement> |
| 41 | + </route> |
| 42 | + </routes> |
| 43 | +
|
| 44 | + .. code-block:: php |
| 45 | +
|
| 46 | + use Symfony\Component\Routing\RouteCollection; |
| 47 | + use Symfony\Component\Routing\Route; |
| 48 | +
|
| 49 | + $collection = new RouteCollection(); |
| 50 | + $collection->add('secure', new Route('/hello/{name}', array( |
| 51 | + '_controller' => 'AcmeDemoBundle:Demo:hello', |
| 52 | + ), array( |
| 53 | + 'name' => '.+', |
| 54 | + ))); |
| 55 | +
|
| 56 | + return $collection; |
| 57 | +
|
| 58 | + .. code-block:: annotation |
| 59 | +
|
| 60 | + use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
| 61 | +
|
| 62 | + class DemoController |
| 63 | + { |
| 64 | + /** |
| 65 | + * @Route("/hello/{name}", name="_hello", requirements={"name" = ".+"}) |
| 66 | + */ |
| 67 | + public function helloAction($name) |
| 68 | + { |
| 69 | + // ... |
| 70 | + } |
| 71 | + } |
0 commit comments