From 342284c09a8c1b92e70b21a7d3a6e0c27da1685b Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 21 Apr 2016 10:13:40 +0200 Subject: [PATCH 1/2] Showed an example of using container parameters in route requirements --- book/routing.rst | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/book/routing.rst b/book/routing.rst index aa08421c458..bb2bcac91e6 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -808,6 +808,101 @@ Path Parameters ``/es`` *won't match this route* ======= ======================== +.. tip:: + + The route requirements can also be defined as container parameters, which + comes in handy when the regular expression is very complex and used time and + again in your application: + + First, define the regular expression as a parameter: + + .. configuration-block:: + + .. code-block:: yaml + + # app/config/config.yml + parameters: + uuid_regex: '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' + + .. code-block:: xml + + + + + + + [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} + + + + .. code-block:: php + + // app/config/config.php + $container->setParameter('uuid_regex', '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'); + + Then, use this ``uuid_regex`` parameter in the route requirements: + + .. configuration-block:: + + .. code-block:: php-annotations + + // src/AppBundle/Controller/MainController.php + + // ... + class MainController extends Controller + { + /** + * @Route("/show/{uuid}", requirements={ + * "uuid": "%uuid_regex%" + * }) + */ + public function showAction($uuid) + { + } + } + + .. code-block:: yaml + + # app/config/routing.yml + show: + path: /show/{uuid} + defaults: { _controller: AppBundle:Main:show } + requirements: + uuid: '%uuid_regex%' + + .. code-block:: xml + + + + + + + AppBundle:Main:show + %uuid_regex% + + + + .. code-block:: php + + // app/config/routing.php + use Symfony\Component\Routing\RouteCollection; + use Symfony\Component\Routing\Route; + + $collection = new RouteCollection(); + $collection->add('show', new Route('/show/{uuid}', array( + '_controller' => 'AppBundle:Main:show', + ), array( + 'uuid' => '%uuid_regex%', + ))); + + return $collection; + .. index:: single: Routing; Method requirement From 1b337e90dfef243160a523c072709f78740a8837 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 21 Apr 2016 11:15:11 +0200 Subject: [PATCH 2/2] Rewords --- book/routing.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/book/routing.rst b/book/routing.rst index bb2bcac91e6..01719dba218 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -812,7 +812,7 @@ Path Parameters The route requirements can also be defined as container parameters, which comes in handy when the regular expression is very complex and used time and - again in your application: + again in your application. First, define the regular expression as a parameter: @@ -843,7 +843,7 @@ Path Parameters // app/config/config.php $container->setParameter('uuid_regex', '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'); - Then, use this ``uuid_regex`` parameter in the route requirements: + Then, use this parameter in the route requirements: .. configuration-block::