Skip to content

Commit a7a7ba8

Browse files
committed
[symfony#1834] Tweaks to new entry about routing parameters
1 parent 30216b9 commit a7a7ba8

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
* :doc:`/cookbook/routing/slash_in_parameter`
9797
* :doc:`/cookbook/routing/redirect_in_config`
9898
* :doc:`/cookbook/routing/method_parameters`
99+
* :doc:`/cookbook/routing/service_container_parameters`
99100

100101
* **symfony1**
101102

cookbook/routing/service_container_parameters.rst

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
.. index::
2-
single: Routing; _service_container_parameters
2+
single: Routing; Service Container Parameters
33

4-
How to use Service Container parameters in your routes
4+
How to use Service Container Parameters in your Routes
55
======================================================
66

77
.. versionadded:: 2.1
8-
This feature was added in Symfony 2.1
8+
The ability to use parameters in your routes was added in Symfony 2.1.
99

10-
Sometimes you may find useful to make some parts of your routes
10+
Sometimes you may find it useful to make some parts of your routes
1111
globally configurable. For instance, if you build an internationalized
1212
site, you'll probably start with one or two locales. Surely you'll
13-
add requirements to avoid a user specify a locale other than those you
14-
support, or simple to increase the power of your routes.
13+
add a requirement to your routes to prevent a user from matching a locale
14+
other than the locales your support.
1515

16-
Suppose that you have a lot of routes and you want to add yet another locale
17-
to your application. If you hardcode the requirements directly in your code,
18-
then you'll need to search everywhere and change the requirements.
19-
20-
Then, why not use a configurable parameter, defined in the Service Container
21-
and make your life easier?
22-
23-
Here you have an example on how to make the ``_locale`` of your routes configurable.
16+
You *could* hardcode your ``_locale`` requirement in all your routes. But
17+
a better solution is to use a configurable service container parameter right
18+
inside your routing configuration:
2419

2520
.. configuration-block::
2621

@@ -60,17 +55,39 @@ Here you have an example on how to make the ``_locale`` of your routes configura
6055
6156
return $collection;
6257
63-
Easy like that, then simply define the ``acme_demo.locales`` parameter in your container.
58+
You can now control and set the ``acme_demo.locales`` parameter somewhere
59+
in your container:
60+
61+
.. configuration-block::
62+
63+
.. code-block:: yaml
64+
65+
# app/config/config.yml
66+
parameters:
67+
acme_demo.locales: en|es
6468
65-
You can also define patterns which use parameters defined in the Service Container.
69+
.. code-block:: xml
70+
71+
<!-- app/config/config.xml -->
72+
<parameters>
73+
<parameter key="acme_demo.locales">en|es</parameter>
74+
</parameters>
75+
76+
.. code-block:: php
77+
78+
# app/config/config.php
79+
$container->setParameter('acme_demo.locales', 'en|es');
80+
81+
You can also use a parameter to define your route pattern (or part of your
82+
pattern):
6683

6784
.. configuration-block::
6885

6986
.. code-block:: yaml
7087
7188
some_route:
72-
pattern: /%acme_demo.parameter_name%
73-
defaults: { _controller: AcmeDemoBundle:Main:index }
89+
pattern: /%acme_demo.route_prefix%/contact
90+
defaults: { _controller: AcmeDemoBundle:Main:contact }
7491
7592
.. code-block:: xml
7693
@@ -80,8 +97,8 @@ You can also define patterns which use parameters defined in the Service Contain
8097
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
8198
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
8299
83-
<route id="some_route" pattern="/%acme_demo.parameter_name%">
84-
<default key="_controller">AcmeDemoBundle:Main:index</default>
100+
<route id="some_route" pattern="/%acme_demo.route_prefix%/contact">
101+
<default key="_controller">AcmeDemoBundle:Main:contact</default>
85102
</route>
86103
</routes>
87104
@@ -91,12 +108,14 @@ You can also define patterns which use parameters defined in the Service Contain
91108
use Symfony\Component\Routing\Route;
92109
93110
$collection = new RouteCollection();
94-
$collection->add('some_route', new Route('/%acme_demo.parameter_name%', array(
111+
$collection->add('some_route', new Route('/%acme_demo.route_prefix%/contact', array(
95112
'_controller' => 'AcmeDemoBundle:Main:contact',
96113
)));
97114
98115
return $collection;
99116
100117
.. note::
101-
You can escape a parameter by doubling the ``%``, e.g. ``/%%acme_demo.parameter_name%%``
102118

119+
Just like in normal service container configuration files, if you actually
120+
need a ``%`` in your route, you can escape the percent sign by doubling
121+
it, e.g. ``/score-50%%``, which would resolve to ``/score-50%``.

0 commit comments

Comments
 (0)