From b536d6d01115609b604f4e5eb3db4b82a1839f44 Mon Sep 17 00:00:00 2001 From: "Mario A. Alvarez Garcia" Date: Fri, 19 Oct 2012 08:44:15 -0400 Subject: [PATCH 1/3] Requirements from the Service Container --- book/routing.rst | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/book/routing.rst b/book/routing.rst index c67f93031a6..0f6b43c95f7 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -615,6 +615,61 @@ the regular expression ``(en|fr)``. | /es | *won't match this route* | +-----+--------------------------+ +.. index:: + single: Routing; Requirements service + +Requirements defined in the Service Container +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. versionadded:: 2.1 + This feature was added in Symfony 2.1 + +If, for some reason, you need to define some configurable requirements, you can +use a parameter from the Service Container. For instance, if you have a +``_locale`` parameter in the routes and you like it to be configurable, +you can do this: + +.. configuration-block:: + + .. code-block:: yaml + + contact: + pattern: /{_locale}/contact + defaults: { _controller: AcmeDemoBundle:Main:contact } + requirements: + _locale: %locale% + + .. code-block:: xml + + + + + + + AcmeDemoBundle:Main:contact + %locale% + + + + .. code-block:: php + + use Symfony\Component\Routing\RouteCollection; + use Symfony\Component\Routing\Route; + + $collection = new RouteCollection(); + $collection->add('contact', new Route('/{_locale}/contact', array( + '_controller' => 'AcmeDemoBundle:Main:contact', + ), array( + '_locale' => '%locale%', + ))); + + return $collection; + +Then just define in the container the locale parameter. This is quite useful +if you don't want to search all your code only to change a simple requirement. + .. index:: single: Routing; Method requirement From a3c9dc2c865e2c95c0478b561c1f780d2637872d Mon Sep 17 00:00:00 2001 From: "Mario A. Alvarez Garcia" Date: Fri, 19 Oct 2012 08:57:52 -0400 Subject: [PATCH 2/3] Pattern from the Service Container --- book/routing.rst | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/book/routing.rst b/book/routing.rst index 0f6b43c95f7..3c76c32d5b1 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -417,6 +417,50 @@ match, giving the ``page`` parameter a value of ``2``. Perfect. | /blog/2 | {page} = 2 | +---------+------------+ +Routing with patterns from the Service Container +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. versionadded:: 2.1 + This feature was added in Symfony 2.1 + +You can define patterns using parameters defined in the Service Container. + +.. configuration-block:: + + .. code-block:: yaml + + some_route: + pattern: /%parameter_name% + defaults: { _controller: AcmeDemoBundle:Main:index } + + .. code-block:: xml + + + + + + + AcmeDemoBundle:Main:index + + + + .. code-block:: php + + use Symfony\Component\Routing\RouteCollection; + use Symfony\Component\Routing\Route; + + $collection = new RouteCollection(); + $collection->add('some_route', new Route('/%parameter_name%', array( + '_controller' => 'AcmeDemoBundle:Main:contact', + ))); + + return $collection; + +.. note:: + You can escape a parameter by doubling the ``%``, e.g. ``/%%parameter_name%%`` + .. index:: single: Routing; Requirements From 43efa4695267ae0a05969d554f19f8e9351c1d4c Mon Sep 17 00:00:00 2001 From: "Mario A. Alvarez Garcia" Date: Fri, 19 Oct 2012 09:24:18 -0400 Subject: [PATCH 3/3] Improved the headers text --- book/routing.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/book/routing.rst b/book/routing.rst index 3c76c32d5b1..afb20e69a65 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -417,8 +417,8 @@ match, giving the ``page`` parameter a value of ``2``. Perfect. | /blog/2 | {page} = 2 | +---------+------------+ -Routing with patterns from the Service Container -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Routing with patterns using parameters from the Service Container +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionadded:: 2.1 This feature was added in Symfony 2.1 @@ -662,8 +662,8 @@ the regular expression ``(en|fr)``. .. index:: single: Routing; Requirements service -Requirements defined in the Service Container -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Requirements using parameters from the Service Container +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. versionadded:: 2.1 This feature was added in Symfony 2.1