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

Commit c2d5136

Browse files
committed
Merge pull request #430 from symfony-cmf/routing-configuration
documentation for cleaned up routing
2 parents 2201e15 + 722d7dc commit c2d5136

File tree

8 files changed

+131
-137
lines changed

8 files changed

+131
-137
lines changed

book/simplecms.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ The SimpleCmsBundle provides a class called ``Page`` which extends from the core
3737
``NodeInterface``, so you can use inside the menu. This three-in-one approach is
3838
the key concept behind the bundle.
3939

40+
The mapping of the ``Page`` to a template and controller works as explained in
41+
the :doc:`previous chapter <routing>`.
42+
4043
Creating a new Page
4144
~~~~~~~~~~~~~~~~~~~
4245

@@ -49,7 +52,7 @@ To create a page, use the
4952
use Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr\Page;
5053
use Doctrine\ODM\PHPCR\DocumentManager;
5154

52-
class LoadRoutingData implements FixtureInterface
55+
class LoadSimpleCms implements FixtureInterface
5356
{
5457
/**
5558
* @param DocumentManager $dm
@@ -79,7 +82,7 @@ have the following tree structure:
7982

8083
.. code-block:: text
8184
82-
/cmf/simple/
85+
/cms/simple/
8386
about/
8487
blog/
8588
symfony-cmf-is-great/
@@ -97,7 +100,7 @@ structure, you would do::
97100
use Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr\Page;
98101
use Doctrine\ODM\PHPCR\DocumentManager;
99102

100-
class LoadRoutingData implements FixtureInterface
103+
class LoadSimpleCms implements FixtureInterface
101104
{
102105
/**
103106
* @param DocumentManager $dm
@@ -130,11 +133,11 @@ structure, you would do::
130133

131134
Every PHPCR-ODM document must have a parent document. Parents are never
132135
created automatically, so we use the PHPCR NodeHelper to ensure we have
133-
the root element (``/cmf/simple`` in this case).
136+
the root element (``/cms/simple`` in this case).
134137

135138
.. note::
136139

137-
The Page at ``/cmf/simple`` is created by an
140+
The Page at ``/cms/simple`` is created by an
138141
:ref:`initializer <phpcr-odm-repository-initializers>` of the
139142
SimpleCmsBundle.
140143

bundles/routing/dynamic.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,20 @@ need content, you can just not set it in the route document.
263263
from content instances. When resolving the route, the ``_locale`` gets
264264
into the request and is picked up by the Symfony2 locale system.
265265

266+
Make sure you configure the valid locales in the configuration so that the
267+
bundle can optimally handle locales. The
268+
:ref:`configuration reference <reference-config-routing-locales>` lists
269+
some options to tweak behaviour and performance.
270+
266271
.. note::
267272

268-
Under PHPCR-ODM, Routes should never be translatable documents, as one
273+
Under PHPCR-ODM, Routes should not be translatable documents, as one
269274
Route document represents one single url, and serving several translations
270275
under the same url is not recommended.
271276

272-
If you need translated URLs, make the locale part of the route name and use
273-
several routes for the same content. The route generator will pick the
274-
correct route if available.
277+
If you need translated URLs, make the ``locale`` part of the route name and
278+
create one route per language for the same content. The route generator will
279+
pick the correct route if available.
275280

276281
Sonata Doctrine PHPCR-ODM Admin classes
277282
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -288,6 +293,10 @@ get an error if Sonata becomes unavailable.
288293
Sonata admin is using the ``content_basepath`` to show the tree of content to
289294
select the route target.
290295

296+
The root path to add Routes defaults to the first entry in ``route_basepaths``,
297+
but you can overwrite this with the ``admin_basepath`` if you need a different
298+
base path.
299+
291300
.. configuration-block::
292301

293302
.. code-block:: yaml

bundles/routing/dynamic_customize.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ You can add your own :ref:`RouteEnhancerInterface <bundles-routing-dynamic_route
2424
implementations if you have a case not handled by the
2525
:ref:`provided enhancers <component-routing-enhancers>`. Simply define services
2626
for your enhancers and tag them with ``dynamic_router_route_enhancer`` to have
27-
them added to the routing.
27+
them added to the routing. You can specify an optional ``priority`` parameter
28+
on the tag to control the order in which enhancers are executed. The higher the
29+
priority, the earlier the enhancer is executed.
2830

2931
.. index:: Route Provider
3032
.. _bundle-routing-custom_provider:

bundles/simple_cms/multilang.rst

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Multi-Language Support
55
----------------------
66

7-
Setting ``addLocalePattern`` to ``true`` in a
7+
Setting the option ``add_locale_pattern`` to ``true`` in a
88
``Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr\Page`` document will
99
result in prefixing the associated route with ``/{_locale}``. Using the native
1010
translation capabilities of PHPCR ODM it is now possible to create different
@@ -42,10 +42,8 @@ For example::
4242
will have the same node name for all languages. So a url
4343
``http://foo.com/en/hello-world`` for english content will look like
4444
``http://foo.com/de/hello-world`` for german content.
45-
46-
At times it might be most feasible to use integers as the node names and
47-
simple append the title of the node in the given locale as an anchor. So
48-
for example ``http://foo.com/de/1#my title`` and
49-
``http://foo.com/de/1#mein title``. If you need language specific URLs,
50-
you want to use the CMF RoutingBundle and ContentBundle directly to have
51-
a separate route document per language.
45+
46+
If you need language specific URLs, you can either add Route documents for
47+
the other locales and configure the dynamic router to look for routes under
48+
both prefixes. You can also completely separate routing and content by using
49+
the separate documents from the RoutingBundle and ContentBundle.

bundles/simple_cms/rendering.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
Rendering
55
---------
66

7-
You can specify the template to render a page by setting the
8-
``_template`` default or by specifying the controller by setting the
9-
``_controller`` default in the page instance. Alternatively one can
10-
configure the template and controller also via the SimpleCmsBundle
11-
:ref:`routing configuration <config-simple-cms-routing>`.
7+
You can configure a mapping of document class to template and/or controller
8+
by configuring the :ref:`RoutingBundle <reference-config-routing-dynamic>`.
9+
When you need specific settings for a single page, you can call
10+
``setDefault()`` for the key ``_template`` or ``_controller`` default in the
11+
page instance.
1212

1313
A simple example for such a template could look like this:
1414

components/routing/nested_matcher.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ do any elaborate matching operations yet - this will be done in the later steps.
3737
implementations for everything needed to get this component running with
3838
Doctrine PHPCR-ODM as well as with the Doctrine ORM.
3939

40+
.. tip::
41+
42+
This component provides the ``Candidates`` implementation for the very
43+
first step of splitting the URL on the ``/`` to allow matching with
44+
variable patterns.
45+
4046
To create and register your own Route Provider, create a class implementing
4147
``Symfony\Cmf\Component\Routing\RouteProviderInterface`` which will have the
4248
following methods::

reference/configuration/routing.rst

Lines changed: 84 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,11 @@ phpcr
305305
phpcr:
306306
enabled: false
307307
manager_name: ~
308-
route_basepath: /cms/routes
308+
route_basepaths:
309+
- /cms/routes
310+
- /cms/simple
309311
content_basepath: /cms/content
312+
admin_basepath: /cms/routes
310313
use_sonata_admin: auto
311314
312315
.. code-block:: xml
@@ -320,10 +323,13 @@ phpcr
320323
<phpcr
321324
enabled="false"
322325
manager-name="null"
323-
route-basepath="/cms/routes"
324326
content-basepath="/cms/content"
327+
admin-basepath="/cms/routes"
325328
use-sonata-admin="auto"
326-
/>
329+
>
330+
<route-basepath>/cms/routes</route-basepath>
331+
<route-basepath>/cms/simple</route-basepath>
332+
</phpcr>
327333
</persistence>
328334
</dynamic>
329335
</config>
@@ -337,8 +343,12 @@ phpcr
337343
'phpcr' => array(
338344
'enabled' => false,
339345
'manager_name' => null,
340-
'route_basepath' => '/cms/routes',
346+
'route_basepaths' => array(
347+
'/cms/routes',
348+
'/cms/simple',
349+
)
341350
'content_basepath' => '/cms/content',
351+
'admin_basepath' => '/cms/routes',
342352
'use_sonata_admin' => 'auto',
343353
),
344354
),
@@ -356,27 +366,37 @@ manager_name
356366

357367
.. include:: partials/persistence_phpcr_manager_name.rst.inc
358368

359-
route_basepath
360-
**************
369+
route_basepaths
370+
***************
361371

362-
**type**: ``string`` **default**: ``/cms/routes``
372+
**type**: ``array`` **default**: ``array('/cms/routes')``
363373

364-
The basepath for routes in the PHPCR tree.
374+
The basepaths where to look for routes in the PHPCR tree.
365375

366-
If the :doc:`CoreBundle <../../bundles/core/index>` is registered, this will default to
367-
``%cmf_core.persistence.phpcr.basepath%/routes``.
376+
If the :doc:`CoreBundle <../../bundles/core/index>` is registered, this will
377+
default to ``%cmf_core.persistence.phpcr.basepath%/routes``. If the
378+
:doc:`SimpleCmsBundle <../../bundles/simple_cms/index>` is registered as well,
379+
this will additionally default to ``%cmf_core.persistence.phpcr.basepath%/simple``.
368380

369381
content_basepath
370382
****************
371383

372-
**type**: ``content_basepath`` **default**: ``/cms/content``
384+
**type**: ``string`` **default**: ``/cms/content``
373385

374386
The basepath for content objects in the PHPCR tree. This information is used
375387
with the sonata admin to offer the correct subtree to select content documents.
376388

377389
If the :doc:`CoreBundle <../../bundles/core/index>` is registered, this will default to
378390
``%cmf_core.persistence.phpcr.basepath%/content``.
379391

392+
admin_basepath
393+
**************
394+
395+
**type**: ``string`` **default**: first value of route_basepaths
396+
397+
The path at which to create routes with Sonata admin. There can be additional
398+
route basepaths, but you will need your own tools to edit those.
399+
380400
use_sonata_admin
381401
****************
382402

@@ -487,12 +507,62 @@ content repository service.
487507
This can be overriden by this option. ORM doesn't have a content
488508
repository at the moment.
489509

510+
.. _reference-config-routing-locales:
511+
490512
locales
491513
~~~~~~~
492514

493-
**type**: ``array`` **default**:
515+
**type**: ``array`` **default**: ``array()``
494516

495517
To enable multilanguage, set the valid locales in this option.
496518

497-
If the :doc:`CoreBundle <../../bundles/core/index>` is registered, this will default to the value
498-
of ``cmf_core.locales``.
519+
If the :doc:`CoreBundle <../../bundles/core/index>` is registered, this will
520+
default to the value of ``cmf_core.locales``.
521+
522+
limit_candidates
523+
~~~~~~~~~~~~~~~~
524+
525+
**type**: ``integer`` **default**: ``20``
526+
527+
With this flag you can tune the routing behaviour when using the dynamic
528+
pattern part of routes stored in the database. If you do never use the variable
529+
pattern field of the Route model, you can set this to 1 as a small performance
530+
optimization. If you have very complex URLs with patterns, you might need to
531+
increase the limit.
532+
533+
.. caution::
534+
535+
Setting this to a higher makes your site more vulnerable to load attacks
536+
when someone visits your site with URLs with lots of slashes in them, since
537+
every slash will lead to a document being tried to be loaded.
538+
539+
match_implicit_locale
540+
~~~~~~~~~~~~~~~~~~~~~
541+
542+
**type**: ``boolean`` **default**: ``true``
543+
544+
Whether the route provider should look for routes without the locale.
545+
546+
For example, when the ``locales`` are ``de`` and ``en`` and the request has the
547+
url ``de/my/path``, the route provider will not only look for ``de/my/path``,
548+
``de/my`` and ``de`` but also for ``my/path`` and ``my``. This allows to use a
549+
single route for multiple languages. This is used for example by the
550+
:doc:`SimpleCms <../../bundles/simple_cms/index>`.
551+
552+
If you do not need this, disabling the option will gain some performance.
553+
554+
auto_locale_pattern
555+
~~~~~~~~~~~~~~~~~~~
556+
557+
**type**: ``boolean`` **default**: ``false``
558+
559+
If you enable this option, the LocaleListener will ensure that routes that have
560+
no locale in their static pattern get the ``auto_locale_pattern`` option set.
561+
562+
.. note::
563+
564+
Enabling this option will prevent you from having any CMF Routes that match
565+
URL without a locale in it.
566+
567+
This is ignored if there are no ``locales`` configured. It makes no sense to
568+
enable this option when ``match_implicit_locale`` is disabled.

0 commit comments

Comments
 (0)