@@ -5,47 +5,47 @@ How to Allow a "/" Character in a Route Parameter
5
5
=================================================
6
6
7
7
Sometimes, you need to compose URLs with parameters that can contain a slash
8
- ``/ ``. For example, take the classic ``/hello/{username } `` route. By default,
9
- ``/hello/Fabien `` will match this route but not `` /hello/Fabien/Kris `` . This
10
- is because Symfony uses this character as separator between route parts.
8
+ ``/ ``. For example, consider the ``/share/{token } `` route. If the `` token ``
9
+ value contains a ``/ `` character this route won't match . This is because Symfony
10
+ uses this character as separator between route parts.
11
11
12
- This guide covers how you can modify a route so that `` /hello/Fabien/Kris ``
13
- matches the ``/hello/{username} `` route, where `` {username} `` equals `` Fabien/Kris `` .
12
+ This article explains how you can modify a route definition so that placeholders
13
+ can contain the ``/ `` character too .
14
14
15
15
Configure the Route
16
16
-------------------
17
17
18
- By default, the Symfony Routing component requires that the parameters
19
- match the following regex path : ``[^/]+ ``. This means that all characters
20
- are allowed except ``/ ``.
18
+ By default, the Symfony Routing component requires that the parameters match
19
+ the following regular expression : ``[^/]+ ``. This means that all characters are
20
+ allowed except ``/ ``.
21
21
22
- You must explicitly allow ``/ `` to be part of your parameter by specifying
23
- a more permissive regex path.
22
+ You must explicitly allow ``/ `` to be part of your placeholder by specifying
23
+ a more permissive regular expression for it:
24
24
25
25
.. configuration-block ::
26
26
27
27
.. code-block :: php-annotations
28
28
29
29
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
30
30
31
- class DemoController
31
+ class DefaultController
32
32
{
33
33
/**
34
- * @Route("/hello/{username }", name="_hello ", requirements={"username "=".+"})
34
+ * @Route("/share/{token }", name="share ", requirements={"token "=".+"})
35
35
*/
36
- public function helloAction($username )
36
+ public function shareAction($token )
37
37
{
38
38
// ...
39
39
}
40
40
}
41
41
42
42
.. code-block :: yaml
43
43
44
- _hello :
45
- path : /hello/{username }
46
- defaults : { _controller: AppBundle:Demo:hello }
44
+ share :
45
+ path : /share/{token }
46
+ defaults : { _controller: AppBundle:Default:share }
47
47
requirements :
48
- username : .+
48
+ token : .+
49
49
50
50
.. code-block :: xml
51
51
@@ -55,9 +55,9 @@ a more permissive regex path.
55
55
xsi : schemaLocation =" http://symfony.com/schema/routing
56
56
http://symfony.com/schema/routing/routing-1.0.xsd" >
57
57
58
- <route id =" _hello " path =" /hello/{username }" >
59
- <default key =" _controller" >AppBundle:Demo:hello </default >
60
- <requirement key =" username " >.+</requirement >
58
+ <route id =" share " path =" /share/{token }" >
59
+ <default key =" _controller" >AppBundle:Default:share </default >
60
+ <requirement key =" token " >.+</requirement >
61
61
</route >
62
62
</routes >
63
63
@@ -67,12 +67,20 @@ a more permissive regex path.
67
67
use Symfony\Component\Routing\Route;
68
68
69
69
$collection = new RouteCollection();
70
- $collection->add('_hello ', new Route('/hello/{username }', array(
71
- '_controller' => 'AppBundle:Demo:hello ',
70
+ $collection->add('share ', new Route('/share/{token }', array(
71
+ '_controller' => 'AppBundle:Default:share ',
72
72
), array(
73
- 'username ' => '.+',
73
+ 'token ' => '.+',
74
74
)));
75
75
76
76
return $collection;
77
77
78
- That's it! Now, the ``{username} `` parameter can contain the ``/ `` character.
78
+ That's it! Now, the ``{token} `` parameter can contain the ``/ `` character.
79
+
80
+ .. note ::
81
+
82
+ If the route defines several placeholders and you apply this permissive
83
+ regular expression to all of them, the results won't be the expected. For
84
+ example, if the route definition is ``/share/{path}/{token} `` and both
85
+ ``path `` and ``token `` accept ``/ ``, then ``path `` will contain its contents
86
+ and the token, and ``token `` will be empty.
0 commit comments