Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 94b3049

Browse files
committed
Merge pull request #706 from symfony-cmf/better-explain-variable-pattern
better explain using variable patterns with the route document
2 parents d9a107a + 28ade3e commit 94b3049

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

bundles/routing/dynamic.rst

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ This will make the routing put the document into the request parameters and if
258258
your controller specifies a parameter called ``$contentDocument``, it will be
259259
passed this document.
260260

261-
You can also use variable patterns for the URL and define requirements and
262-
defaults::
261+
You can also use variable patterns for the URL and define requirements with
262+
``setRequirement`` and defaults with ``setDefault``::
263263

264264
// do not forget leading slash if you want /projects/{id} and not /projects{id}
265265
$route->setVariablePattern('/{id}');
@@ -277,19 +277,24 @@ it gets chosen. Otherwise, routing checks if ``/routes/projects`` has a pattern
277277
that matches. If not, the top document at ``/routes`` is checked for a matching
278278
pattern.
279279

280-
Of course you can also have several parameters, as with normal Symfony
281-
routes. The semantics and rules for patterns, defaults and requirements are
282-
exactly the same as in core routes.
280+
The semantics and rules for patterns, defaults and requirements are exactly the
281+
same as in core routes. If you have several parameters, or static bits *after*
282+
a parameter, make them part of the variable pattern::
283+
284+
$route->setVariablePattern('/{context}/item/{id}');
285+
$route->setRequirement('context', '[a-z]+');
286+
$route->setRequirement('id', '\d+');
283287

284288
.. note::
285289

286290
The ``RouteDefaultsValidator`` validates the route defaults parameters.
287291
For more information, see :ref:`bundle-routing-route-defaults-validator`.
288292

289-
Your controller can expect the ``$id`` parameter as well as the ``$contentDocument``
290-
as you set a content on the route. The content could be used to define an intro
291-
section that is the same for each project or other shared data. If you don't
292-
need content, you can just not set it in the route document.
293+
With the above example, your controller can expect both the ``$id`` parameter
294+
as well as the ``$contentDocument`` if you set a content on the route and have
295+
a variable pattern with ``{id}``. The content could be used to define an intro
296+
section that is the same for each id. If you don't need content, you can also
297+
omit setting a content document on the route document.
293298

294299
.. _component-route-generator-and-locales:
295300

0 commit comments

Comments
 (0)