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

Commit 7ba6c1b

Browse files
committed
Merge pull request #288 from symfony-cmf/routing
minor tweaks to the routing book chapter
2 parents 148387f + 29e902c commit 7ba6c1b

File tree

4 files changed

+109
-145
lines changed

4 files changed

+109
-145
lines changed

book/routing.rst

Lines changed: 50 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ their configured priority:
6262
cmf_routing:
6363
chain:
6464
routers_by_id:
65-
# enable the DynamicRouter with high priority to allow overwriting
66-
# configured routes with content
67-
cmf_routing.dynamic_router: 200
65+
# enable the DynamicRouter with a low priority
66+
# this way the non dynamic routes take precedence
67+
# to prevent needless database look ups
68+
cmf_routing.dynamic_router: 20
6869
69-
# enable the symfony default router with a lower priority
70+
# enable the symfony default router with a higher priority
7071
router.default: 100
7172
7273
.. code-block:: xml
@@ -78,14 +79,15 @@ their configured priority:
7879
7980
<config xmlns="http://cmf.symfony.com/schema/dic/routing">
8081
<chain>
81-
<!-- enable the DynamicRouter with high priority to allow overwriting
82-
configured routes with content -->
82+
<!-- enable the DynamicRouter with a low priority
83+
this way the non dynamic routes take precedence
84+
to prevent needless database look ups -->
8385
<routers-by-id
8486
id="cmf_routing.dynamic_router">
85-
200
87+
20
8688
</routers-by-id>
8789
88-
<!-- enable the symfony default router with a lower priority -->
90+
<!-- enable the symfony default router with a higher priority -->
8991
<routers-by-id
9092
id="router.default">
9193
100
@@ -99,12 +101,13 @@ their configured priority:
99101
$container->loadFromExtension('cmf_routing', array(
100102
'chain' => array(
101103
'routers_by_id' => array(
102-
// enable the DynamicRouter with high priority to allow overwriting
103-
// configured routes with content
104-
'cmf_routing.dynamic_router' => 200,
104+
// enable the DynamicRouter with a low priority
105+
// this way the non dynamic routes take precedence
106+
// to prevent needless database look ups
107+
'cmf_routing.dynamic_router' => 20,
105108
106-
// enable the symfony default router with a lower priority
107-
'router.default' => 100,
109+
// enable the symfony default router with a higher priority
110+
'router.default' => 100,
108111
),
109112
),
110113
));
@@ -146,21 +149,23 @@ The Default Symfony2 Router
146149
---------------------------
147150

148151
Although it replaces the default routing mechanism, Symfony CMF Routing allows
149-
you to keep using the existing system. In fact, the default routing is enabled
150-
by default, so you can keep using the routes you declared in your
152+
you to keep using the existing system. In fact, the standard Symfony2 routing
153+
is enabled by default, so you can keep using the routes you declared in your
151154
configuration files, or as declared by other bundles.
152155

153156
.. _start-routing-dynamic-router:
154157

155158
The DynamicRouter
156159
-----------------
157160

158-
This Router can dynamically load Route instances from a given provider. It
159-
then uses a matching process to the incoming request to a specific Route,
160-
which in turn is used to determine which Controller to forward the request to.
161+
This Router can dynamically load ``Route`` instances from a dynamic source via
162+
a so called *provider*. In fact it only loads candidate routes. The actual
163+
matching process is exactly the same as with the standard Symfony2 routing
164+
mechanism. However the ``DynamicRouter`` additionally is able to determine
165+
which Controller and Template to use based on the ``Route`` that is matched.
161166

162-
The bundle's default configuration states that ``DynamicRouter`` is disabled
163-
by default. To activate it, just add the following to your configuration file:
167+
By default the ``DynamicRouter`` is disabled. To activate it, just add the
168+
following to your configuration file:
164169

165170
.. configuration-block::
166171

@@ -194,7 +199,7 @@ by default. To activate it, just add the following to your configuration file:
194199
));
195200
196201
This is the minimum configuration required to load the ``DynamicRouter`` as a
197-
service, thus making it capable of performing any routing. Actually, when you
202+
service, thus making it capable of performing routing. Actually, when you
198203
browse the default pages that come with the Symfony CMF SE, it is the
199204
``DynamicRouter`` that matches your requests with the Controllers and
200205
Templates.
@@ -205,11 +210,13 @@ Getting the Route Object
205210
~~~~~~~~~~~~~~~~~~~~~~~~
206211

207212
The provider to use can be configured to best suit each implementation's
208-
needs, and must implement the ``RouteProviderInterface``. As part of this
209-
bundle, an implementation for `PHPCR-ODM`_ is provided. Also, you can easily
210-
create your own, as the Router itself is storage agnostic. The default
211-
provider loads the route at the path in the request and all parent paths to
212-
allow for some of the path segments being parameters.
213+
needs. As part of this bundle, an implementation for `Doctrine ORM`_ and
214+
`PHPCR-ODM`_ is provided. Also, you can easily create your own by simply
215+
implementing the ``RouteProviderInterface``. Providers are responsible
216+
for fetching an ordered subset of candidate routes that could match the
217+
request. For example the default `PHPCR-ODM`_ provider loads the ``Route``
218+
at the path in the request and all parent paths to allow for some of the
219+
path segments being parameters.
213220

214221
For more detailed information on this implementation and how you can customize
215222
or extend it, refer to :doc:`../bundles/routing/introduction`.
@@ -237,23 +244,23 @@ A Route needs to specify which Controller should handle a specific Request.
237244
The ``DynamicRouter`` uses one of several possible methods to determine it (in
238245
order of precedence):
239246

240-
* Explicit: The stored Route document itself can explicitly declare the target
241-
Controller by specifying the '_controller' value in ``getRouteDefaults()``.
242-
* By alias: the Route returns a 'type' value in ``getRouteDefaults()``,
247+
* Explicit: The ``Route`` document itself can explicitly declare the target
248+
Controller if one is returned from ``getDefault('_controller')``.
249+
* By alias: The ``Route`` document returns a value from ``getDefault('type')``,
243250
which is then matched against the provided configuration from config.yml
244-
* By class: requires the Route instance to implement ``RouteObjectInterface``
245-
and return an object for ``getRouteContent()``. The returned class type is
251+
* By class: Requires the ``Route`` document to implement ``RouteObjectInterface``
252+
and return an object for ``getContent()``. The returned class type is
246253
then matched against the provided configuration from config.yml.
247-
* Default: if configured, a default Controller will be used.
254+
* Default: If configured, a default Controller will be used.
248255

249256
Apart from this, the ``DynamicRouter`` is also capable of dynamically
250257
specifying which Template will be used, in a similar way to the one used to
251258
determine the Controller (in order of precedence):
252259

253260
* Explicit: The stored Route document itself can explicitly declare the target
254-
Template in ``getRouteDefaults()``.
255-
* By class: requires the Route instance to implement ``RouteObjectInterface``
256-
and return an object for ``getRouteContent()``. The returned class type is
261+
Template by returning the name of the template via ``getDefaults('_template')``.
262+
* By class: Requires the Route instance to implement ``RouteObjectInterface``
263+
and return an object for ``getContent()``. The returned class type is
257264
then matched against the provided configuration from config.yml.
258265

259266
Here's an example of how to configure the above mentioned options:
@@ -267,7 +274,7 @@ Here's an example of how to configure the above mentioned options:
267274
dynamic:
268275
generic_controller: cmf_content.controller:indexAction
269276
controllers_by_type:
270-
editablestatic: sandbox_main.controller:indexAction
277+
editable_static: sandbox_main.controller:indexAction
271278
controllers_by_class:
272279
Symfony\Cmf\Bundle\ContentBundle\Document\StaticContent: cmf_content.controller::indexAction
273280
templates_by_class:
@@ -282,8 +289,8 @@ Here's an example of how to configure the above mentioned options:
282289
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
283290
284291
<config xmlns="http://cmf.symfony.com/schema/dic/routing">
285-
<dynamic generic-controller="cmf_content.controllerindexAction">
286-
<controllers-by-type type="editablestatic">
292+
<dynamic generic-controller="cmf_content.controller:indexAction">
293+
<controllers-by-type type="editable_static">
287294
sandbox_main.controller:indexAction
288295
</controllers-by-type>
289296
@@ -308,7 +315,7 @@ Here's an example of how to configure the above mentioned options:
308315
'dynamic' => array(
309316
'generic_controller' => 'cmf_content.controller:indexAction',
310317
'controllers_by_type' => array(
311-
'editablestatic' => 'sandbox_main.controller:indexAction',
318+
'editable_static' => 'sandbox_main.controller:indexAction',
312319
),
313320
'controllers_by_class' => array(
314321
'Symfony\Cmf\Bundle\ContentBundle\Document\StaticContent' => 'cmf_content.controller::indexAction',
@@ -338,8 +345,7 @@ Linking a Route with a Model Instance
338345
Depending on your application's logic, a requested URL may have an associated
339346
model instance from the database. Those Routes can implement the
340347
``RouteObjectInterface``, and optionally return a model instance, that will be
341-
automatically passed to the Controller as the ``$contentDocument`` variable,
342-
if declared as parameter.
348+
automatically passed to the Controller as the ``contentDocument`` method parameter.
343349

344350
Note that a Route can implement the above mentioned interface but still not
345351
return any model instance, in which case no associated object will be provided.
@@ -367,7 +373,7 @@ configured as follows:
367373
cmf_routing:
368374
dynamic:
369375
controllers_by_class:
370-
Symfony\Cmf\Component\Routing\RedirectRouteInterface: cmf_routing.redirect_controller:redirectAction
376+
Symfony\Cmf\Component\Routing\RedirectRouteInterface: cmf_routing.redirect_controller:redirectAction
371377
372378
.. code-block:: xml
373379
@@ -464,96 +470,6 @@ in the document.
464470
For more details, see the
465471
:ref:`route document section in the RoutingBundle documentation <bundle-routing-document>`.
466472

467-
Integrating with SonataAdmin
468-
----------------------------
469-
470-
If ``sonata-project/doctrine-phpcr-admin-bundle`` is added to the
471-
composer.json require section, the route documents are exposed in the
472-
SonataDoctrinePhpcrAdminBundle. For instructions on how to configure this
473-
Bundle see :doc:`../bundles/doctrine_phpcr_admin`.
474-
475-
By default, ``use_sonata_admin`` is automatically set based on whether the
476-
SonataDoctrinePhpcrAdminBundle is available but you can explicitly disable it
477-
to not have it even if Sonata is enabled, or explicitly enable to get an error
478-
if Sonata becomes unavailable.
479-
480-
There are a couple of configuration options for the admin. The
481-
``content_basepath`` points to the root of your content documents.
482-
483-
.. configuration-block::
484-
485-
.. code-block:: yaml
486-
487-
# app/config/config.yml
488-
cmf_routing:
489-
dynamic:
490-
persistence:
491-
phpcr:
492-
# use true/false to force using / not using sonata admin
493-
use_sonata_admin: auto
494-
495-
# used with Sonata Admin to manage content; defaults to cmf_core.content_basepath
496-
content_basepath: ~
497-
498-
.. code-block:: xml
499-
500-
<!-- app/config/config.xml -->
501-
<?xml version="1.0" encoding="UTF-8" ?>
502-
503-
<container xmlns="http://cmf.symfony.com/schema/dic/services"
504-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
505-
506-
<config xmlns="http://cmf.symfony.com/schema/dic/routing">
507-
<dynamic>
508-
<persistence>
509-
<!-- use-sonata-admin: use true/false to force using / not using sonata admin -->
510-
<!-- content-basepath: used with Sonata Admin to manage content;
511-
defaults to cmf_core.content_basepath -->
512-
<phpcr
513-
use-sonata-admin="auto"
514-
content-basepath="null"
515-
/>
516-
</persistence>
517-
</dynamic>
518-
</config>
519-
</container>
520-
521-
.. code-block:: php
522-
523-
// app/config/config.php
524-
$container->loadFromExtension('cmf_routing', array(
525-
'dynamic' => array(
526-
'persistence' => array(
527-
'phpcr' => array(
528-
// use true/false to force using / not using sonata admin
529-
'use_sonata_admin' => 'auto',
530-
531-
// used with Sonata Admin to manage content; defaults to cmf_core.content_basepath
532-
'content_basepath' => null,
533-
),
534-
),
535-
),
536-
));
537-
538-
Terms Form Type
539-
---------------
540-
541-
The Routing bundle defines a form type that can be used for classical "accept terms"
542-
checkboxes where you place URLs in the label. Simply specify
543-
``cmf_routing_terms_form_type`` as the form type name and
544-
specify a label and an array with ``content_ids`` in the options::
545-
546-
$form->add('terms', 'cmf_routing_terms_form_type', array(
547-
'label' => 'I have seen the <a href="%team%">Team</a> and <a href="%more%">More</a> pages ...',
548-
'content_ids' => array(
549-
'%team%' => '/cms/content/static/team',
550-
'%more%' => '/cms/content/static/more'
551-
),
552-
));
553-
554-
The form type automatically generates the routes for the specified content and
555-
passes the routes to the trans twig helper for replacement in the label.
556-
557473
Further Notes
558474
-------------
559475

@@ -564,5 +480,6 @@ For more information on the Routing component of Symfony CMF, please refer to:
564480
* Symfony2's `Routing`_ component page
565481
* :doc:`../cookbook/handling_multilang_documents` for some notes on multilingual routing
566482

567-
.. _`PHPCR-ODM`: https://github.com/doctrine/phpcr-odm
483+
.. _`Doctrine ORM`: http://www.doctrine-project.org/projects/orm.html
484+
.. _`PHPCR-ODM`: http://www.doctrine-project.org/projects/phpcr-odm.html
568485
.. _`Routing`: http://symfony.com/doc/current/components/routing/introduction.html

bundles/routing/dynamic.rst

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,65 @@ get an error if Sonata becomes unavailable.
267267
Sonata admin is using the ``content_basepath`` to show the tree of content to
268268
select the route target.
269269

270-
See the :ref:`routing configuration reference PHPCR section <reference-configuration-routing-persistence-phpcr>`
271-
for more details.
270+
.. configuration-block::
271+
272+
.. code-block:: yaml
273+
274+
# app/config/config.yml
275+
cmf_routing:
276+
dynamic:
277+
persistence:
278+
phpcr:
279+
# use true/false to force using / not using sonata admin
280+
use_sonata_admin: auto
281+
282+
# used with Sonata Admin to manage content; defaults to %cmf_core.basepath%/content
283+
content_basepath: ~
284+
285+
.. code-block:: xml
286+
287+
<!-- app/config/config.xml -->
288+
<?xml version="1.0" encoding="UTF-8" ?>
289+
290+
<container xmlns="http://cmf.symfony.com/schema/dic/services"
291+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
292+
293+
<config xmlns="http://cmf.symfony.com/schema/dic/routing">
294+
<dynamic>
295+
<persistence>
296+
<!-- use-sonata-admin: use true/false to force using / not using sonata admin -->
297+
<!-- content-basepath: used with Sonata Admin to manage content;
298+
defaults to %cmf_core.basepath%/content -->
299+
<phpcr
300+
use-sonata-admin="auto"
301+
content-basepath="null"
302+
/>
303+
</persistence>
304+
</dynamic>
305+
</config>
306+
</container>
307+
308+
.. code-block:: php
309+
310+
// app/config/config.php
311+
$container->loadFromExtension('cmf_routing', array(
312+
'dynamic' => array(
313+
'persistence' => array(
314+
'phpcr' => array(
315+
// use true/false to force using / not using sonata admin
316+
'use_sonata_admin' => 'auto',
317+
318+
// used with Sonata Admin to manage content; defaults to %cmf_core.basepath%/content
319+
'content_basepath' => null,
320+
),
321+
),
322+
),
323+
));
272324
273325
Doctrine ORM integration
274326
------------------------
275327

276-
Alternatively, you can use the Doctrine ORM provider by specifying the
328+
Alternatively, you can use the `Doctrine ORM`_ provider by specifying the
277329
``persistence.orm`` part of the configuration. It does a similar job but, as
278330
the name indicates, loads ``Route`` entities from an ORM database.
279331

@@ -487,6 +539,7 @@ Read on in the chapter :doc:`customizing the dynamic router <dynamic_customize>`
487539

488540
.. _`CMF sandbox`: https://github.com/symfony-cmf/cmf-sandbox
489541
.. _`CMF Routing component`: https://github.com/symfony-cmf/Routing
490-
.. _`PHPCR-ODM`: https://github.com/doctrine/phpcr-odm
542+
.. _`Doctrine ORM`: http://www.doctrine-project.org/projects/orm.html
543+
.. _`PHPCR-ODM`: http://www.doctrine-project.org/projects/phpcr-odm.html
491544
.. _`Sonata Admin extension documentation`: http://sonata-project.org/bundles/admin/master/doc/reference/extensions.html
492545
.. _`URL generating capabilities of Symfony2`: http://symfony.com/doc/current/book/routing.html#generating-urls

bundles/routing/dynamic_customize.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,4 @@ provider. See `Creating and configuring services in the container`_ for
151151
information on creating custom services.
152152

153153
.. _`Creating and configuring services in the container`: http://symfony.com/doc/current/book/service_container.html#creating-configuring-services-in-the-container/
154-
.. _`PHPCR-ODM`: https://github.com/doctrine/phpcr-odm
154+
.. _`PHPCR-ODM`: http://www.doctrine-project.org/projects/phpcr-odm.html

0 commit comments

Comments
 (0)