From ec9816ba6c243be513d94483991156d5f6493f56 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sat, 10 Jun 2017 16:57:03 +0200 Subject: [PATCH 1/2] Reworded the article about slashes in routing placeholders --- routing/slash_in_parameter.rst | 56 +++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/routing/slash_in_parameter.rst b/routing/slash_in_parameter.rst index a7415fb165b..99e5560ce6d 100644 --- a/routing/slash_in_parameter.rst +++ b/routing/slash_in_parameter.rst @@ -5,22 +5,22 @@ How to Allow a "/" Character in a Route Parameter ================================================= Sometimes, you need to compose URLs with parameters that can contain a slash -``/``. For example, take the classic ``/hello/{username}`` route. By default, -``/hello/Fabien`` will match this route but not ``/hello/Fabien/Kris``. This -is because Symfony uses this character as separator between route parts. +``/``. For example, consider the ``/share/{token}`` route. If the ``token`` +value contains a ``/`` character this route won't match. This is because Symfony +uses this character as separator between route parts. -This guide covers how you can modify a route so that ``/hello/Fabien/Kris`` -matches the ``/hello/{username}`` route, where ``{username}`` equals ``Fabien/Kris``. +This article explains how you can modify a route definition so that placeholders +can contain the ``/`` character too. Configure the Route ------------------- -By default, the Symfony Routing component requires that the parameters -match the following regex path: ``[^/]+``. This means that all characters -are allowed except ``/``. +By default, the Symfony Routing component requires that the parameters match +the following regular expression: ``[^/]+``. This means that all characters are +allowed except ``/``. -You must explicitly allow ``/`` to be part of your parameter by specifying -a more permissive regex path. +You must explicitly allow ``/`` to be part of your placeholder by specifying +a more permissive regular expression for it: .. configuration-block:: @@ -28,12 +28,12 @@ a more permissive regex path. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; - class DemoController + class DefaultController { /** - * @Route("/hello/{username}", name="_hello", requirements={"username"=".+"}) + * @Route("/share/{token}", name="share", requirements={"token"=".+"}) */ - public function helloAction($username) + public function shareAction($token) { // ... } @@ -41,11 +41,11 @@ a more permissive regex path. .. code-block:: yaml - _hello: - path: /hello/{username} - defaults: { _controller: AppBundle:Demo:hello } + share: + path: /share/{token} + defaults: { _controller: AppBundle:Default:share } requirements: - username: .+ + token: .+ .. code-block:: xml @@ -55,9 +55,9 @@ a more permissive regex path. xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - - AppBundle:Demo:hello - .+ + + AppBundle:Default:share + .+ @@ -67,12 +67,20 @@ a more permissive regex path. use Symfony\Component\Routing\Route; $collection = new RouteCollection(); - $collection->add('_hello', new Route('/hello/{username}', array( - '_controller' => 'AppBundle:Demo:hello', + $collection->add('share', new Route('/share/{token}', array( + '_controller' => 'AppBundle:Default:share', ), array( - 'username' => '.+', + 'token' => '.+', ))); return $collection; -That's it! Now, the ``{username}`` parameter can contain the ``/`` character. +That's it! Now, the ``{token}`` parameter can contain the ``/`` character. + +.. note:: + + If the route defines several placeholders and you apply this permissive + regular expression to all of them, the results won't be the expected. For + example, if the route definition is ``/share/{path}/{token}`` and both + ``path`` and ``token`` accept ``/``, then ``path`` will contain its contents + and the token, whereas ``token`` will be empty. From 71aceb21b4111d0e41a80e41eb6325ad90be00d3 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Wed, 14 Jun 2017 21:06:02 -0400 Subject: [PATCH 2/2] minor tweak --- routing/slash_in_parameter.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routing/slash_in_parameter.rst b/routing/slash_in_parameter.rst index 99e5560ce6d..afa24304ee1 100644 --- a/routing/slash_in_parameter.rst +++ b/routing/slash_in_parameter.rst @@ -83,4 +83,4 @@ That's it! Now, the ``{token}`` parameter can contain the ``/`` character. regular expression to all of them, the results won't be the expected. For example, if the route definition is ``/share/{path}/{token}`` and both ``path`` and ``token`` accept ``/``, then ``path`` will contain its contents - and the token, whereas ``token`` will be empty. + and the token, and ``token`` will be empty.