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

Commit c6479f8

Browse files
committed
Merge pull request #221 from WouterJ/rewrote_routing_component
Continued rewriting routing component
2 parents 2a4b8c1 + 0dde272 commit c6479f8

File tree

2 files changed

+83
-88
lines changed

2 files changed

+83
-88
lines changed

bundles/routing.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,24 @@ as we set a content on the route. The content could be used to define an intro
314314
section that is the same for each project or other shared data. If you don't
315315
need content, you can just not set it in the document.
316316

317+
.. _component-route-generator-and-locales:
318+
319+
.. sidebar:: ContentAwareGenerator and locales
320+
321+
You can use the ``_locale`` default value in a Route to create one Route
322+
per locale, all referencing the same multilingual content instance. The
323+
``ContentAwareGenerator`` respects the ``_locale`` when generating routes
324+
from content instances. When resolving the route, the ``_locale`` gets
325+
into the request and is picked up by the Symfony2 locale system.
326+
327+
.. note::
328+
329+
Under PHPCR-ODM, Routes should never be translatable documents, as one
330+
Route document represents one single url, and serving several translations
331+
under the same url is not recommended.
332+
333+
If you need translated URLs, make the locale part of the route name.
334+
317335
Sonata Admin Configuration
318336
--------------------------
319337

components/routing.rst

Lines changed: 65 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ Routing
77

88
The Symfony CMF Routing component extends the Symfony2 core routing
99
component. Even though it has Symfony in its name, it does not need the full
10-
Symfony2 Framework and can be used in standalone projects. For integration
11-
with the Symfony2 Framework, we provide :doc:`../bundles/routing`.
10+
Symfony2 Framework and can be used in standalone projects.
1211

1312
At the core of the Symfony CMF Routing component is the `ChainRouter`_. The
1413
ChainRouter tries to match a request with each of its registered routers,
@@ -162,7 +161,7 @@ parameters from the information provided by the match.
162161
.. _components-routing-events:
163162

164163
Events
165-
~~~~~~
164+
......
166165

167166
Events are dispatched during the Dynamic Router match process. Currently there
168167
are 2 events:
@@ -199,15 +198,14 @@ to use when handling the current Request:
199198
""""""""""""""""""""
200199

201200
Although the ``RouteProviderInterface`` can be used in other ways, it's main
202-
goal is to be easily implemented on top of Doctrine PHPCR ODM or a relational
201+
goal is to be easily implemented on top of Doctrine PHPCR ODM or any other
203202
database, effectively allowing you to store and manage routes dynamically from
204-
database.
203+
the database.
205204

206205
Based on the ``Request``, the ``NestedMatcher`` will retrieve an ordered
207-
collection of ``Route`` objects from the ``RouteProviderInterface``. The idea
208-
of this provider is to provide all routes that could potentially match, but
209-
**not** to do any elaborate matching operations yet - this is the job of the
210-
later steps.
206+
collection of ``Route`` objects from the Route Provider. The idea of this
207+
provider is to provide all routes that could potentially match, but **not** to
208+
do any elaborate matching operations yet - this will be done in the later steps.
211209

212210
The underlying implementation of the ``RouteProviderInterface`` is not in the
213211
scope of this component. The :doc:`RoutingBundle <../bundles/routing>`
@@ -218,6 +216,7 @@ To create and register your own Route Provider, create a class extending
218216
the methods::
219217

220218
use Symfony\Cmf\Component\Routing\RouteProviderInterface;
219+
221220
use Symfony\Component\Routing\RouteCollection;
222221
use Symfony\Component\Routing\Exception\RouteNotFoundException;
223222

@@ -269,7 +268,7 @@ The Route Provider is set using the first argument of the constructor for the
269268
// ...
270269

271270
$routeProvider = new DoctrineOrmRouteProvider(...);
272-
$nestedMatcher = new NestedMatcher($routeProvider);
271+
$nestedMatcher = new NestedMatcher($routeProvider, ...);
273272

274273
2. The Route Filters
275274
""""""""""""""""""""
@@ -279,14 +278,32 @@ implementations to reduce the provided ``Route`` objects, e.g. for doing
279278
content negotiation. It is the responsibility of each filter to throw the
280279
``ResourceNotFoundException`` if no more routes are left in the collection.
281280

281+
Filters are created by implementing
282+
``Symfony\Cmf\Component\Routing\NestedMatcher\RouteFilterInterface``. They can
283+
be registered with the ``addRouteFilter`` method, which has an optional second
284+
argument to set the priority.
285+
282286
3. The Final Matcher
283287
""""""""""""""""""""
284288

285-
The ``FinalMatcherInterface`` implementation has to determine exactly one
286-
Route as the best match or throw an exception if no adequate match could
287-
be found. The default implementation uses the
289+
The ``FinalMatcherInterface`` implementation has to find exactly one Route or
290+
throw an exception if no adequate match could be found. The default
291+
implementation uses the
288292
:class:`Symfony\\Component\\Routing\\Matcher\\UrlMatcher` of the Symfony
289-
Routing Component.
293+
Routing Component and is called
294+
``Symfony\Cmf\Component\Routing\NestedMatcher\UrlMatcher``.
295+
296+
You can create your own final matcher by implementing
297+
``Symfony\Cmf\Component\Routing\NestedMatcher\FinalMatcherInterface``.
298+
299+
The final matcher is set using the second argument of the constructor of the
300+
``NestedMatcher``::
301+
302+
use Symfony\Cmf\Component\Routing\NestedMatcher\UrlMatcher
303+
// ...
304+
305+
$finalMatcher = new UrlMatcher(...);
306+
$nestedMatcher = new NestedMatcher($routeProvider, $finalMatcher);
290307

291308
.. _component-routing-enhancers:
292309

@@ -306,40 +323,43 @@ already provides some simple enhancers:
306323
* `RouteContentEnhancer`_
307324

308325
You can also create your own Route Enhancer by creating a class which
309-
implements ``Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancer``.
326+
implements ``Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancer``. Route
327+
Enhancers are registered using the ``addRouteEnhancer`` method, which has an
328+
optional second argument to provide the priority.
310329

311330
Linking a Route with a Content
312331
..............................
313332

314333
Depending on your application's logic, a requested url may have an associated
315-
content from the database. Those Routes should implement the
316-
``RouteObjectInterface``, and can optionally return a model instance. If you
317-
configure the ``RouteContentEnhancer``, it will included that content in the
318-
match array, with the ``_content`` key. Notice that a Route can implement
319-
the above mentioned interface but still not to return any model instance,
320-
in which case no associated object will be returned.
334+
content object. A route for such URL may implement the ``RouteObjectInterface``
335+
to return a content object if present. If you configure the
336+
``RouteContentEnhancer``, it will included that content in the match array,
337+
using the ``_content`` key. Notice that a Route can implement the above
338+
mentioned interface but still not to return any model instance, in which case
339+
no associated object will be returned.
321340

322341
Furthermore, routes that implement this interface can also provide a custom
323342
Route name. The key returned by ``getRouteKey`` will be used as route name
324343
instead of the Symfony core compatible route name and can contain any
325344
characters. This allows you, for example, to set a path as the route name. Both
326-
UrlMatchers provided with the NestedMatcher replace the _route key with the
327-
route instance and put the provided name into _route_name.
345+
UrlMatchers provided with the ``NestedMatcher`` replace the ``_route`` key
346+
with the route instance and put the provided name into ``_route_name``.
328347

329-
All routes still need to extend the base class ``Symfony\Component\Routing\Route``.
348+
All routes still need to extend the base
349+
:class:`Symfony\\Component\\Routing\\Route <Symfony\\Component\\Routing\\Route>`
350+
class.
330351

331352
Redirections
332353
............
333354

334355
You can build redirections by implementing the ``RedirectRouteInterface``.
335-
It can redirect either to an absolute URI, to a named Route that can be
336-
generated by any Router in the chain or to another Route object provided by the
337-
Route.
356+
It can redirect to an absolute URI, a named Route that can be generated by any
357+
Router in the chain or to another Route object provided by the Route.
338358

339359
Notice that the actual redirection logic is not handled by the bundle. You
340-
should implement your own logic to handle the redirection. For an example on
360+
should implement your own logic to handle the redirection. For an example of
341361
implementing that redirection under the full Symfony2 stack, refer to
342-
:doc:`../bundles/routing`.
362+
:doc:`the RoutingBundle <../bundles/routing>`.
343363

344364
Generating URLs
345365
~~~~~~~~~~~~~~~
@@ -349,20 +369,20 @@ is also responsible for generating an URL from a Route and its parameters.
349369
The ``ChainRouter`` iterates over its known routers until one of them is
350370
able to generate a matching URL.
351371

352-
Apart from using ``RequestMatcherInterface`` or ``UrlMatcherInterface`` to
353-
match a Request/URL to its corresponding parameters, the ``DynamicRouter``
354-
also uses an ``UrlGeneratorInterface`` instance, which allows it to
355-
generate an URL from a Route.
372+
Beside ``RequestMatcherInterface`` and ``UrlMatcherInterface`` to match a
373+
Request/URL to its corresponding parameters, the ``DynamicRouter`` also uses
374+
a ``UrlGeneratorInterface`` instance, which allows it to generate an URL from
375+
a Route.
356376

357377
The included ``ProviderBasedGenerator`` extends Symfony2's default
358-
:class:`Symfony\\Component\\routing\\Generator\\UrlGenerator`
359-
(which, in turn, implements ``UrlGeneratorInterface``) and - if $name is
360-
not already a ``Route`` object - loads the route from the ``RouteProviderInterface``.
361-
It then lets the core logic generate the URL from that Route.
362-
363-
The bundle also include the ``ContentAwareGenerator``, which extends the
364-
``ProviderBasedGenerator`` to check if $name is an object implementing
365-
``RouteAwareInterface`` and, if so, gets the Route from the content.
378+
:class:`Symfony\\Component\\routing\\Generator\\UrlGenerator` (which, in turn,
379+
implements ``UrlGeneratorInterface``) and - if the name is not already a
380+
``Route`` object - loads the Route from the Route provider. It then lets the
381+
core logic generate the URL from that Route.
382+
383+
The component also includes the ``ContentAwareGenerator``, which extends the
384+
``ProviderBasedGenerator`` to check if the name is an object implementing
385+
``RouteReferrersReadInterface`` and, if so, gets the Route from the content.
366386
Using the ``ContentAwareGenerator``, you can generate urls for your content in
367387
three ways:
368388

@@ -371,56 +391,13 @@ three ways:
371391
* Or provide an implementation of ``ContentRepositoryInterface`` and pass the id
372392
of the content object as parameter ``content_id`` and ``null`` as $name.
373393

374-
.. _component-route-generator-and-locales:
375-
376-
ContentAwareGenerator and locales
377-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
378-
379-
You can use the ``_locale`` default value in a Route to create one Route
380-
per locale, all referencing the same multilingual content instance. The ``ContentAwareGenerator``
381-
respects the ``_locale`` when generating routes from content instances. When resolving
382-
the route, the ``_locale`` gets into the request and is picked up by the Symfony2
383-
locale system.
384-
385-
.. note::
386-
387-
Under PHPCR-ODM, Routes should never be translatable documents, as one
388-
Route document represents one single url, and serving several translations
389-
under the same url is not recommended.
390-
391-
If you need translated URLs, make the locale part of the route name.
392-
393-
Customization
394-
-------------
395-
396-
The Routing bundles allows for several customization options, depending on
397-
your specific needs:
398-
399-
* You can implement your own RouteProvider to load routes from a different source
400-
* Your Route parameters can be easily manipulated using the existing Enhancers
401-
* You can also add your own Enhancers to the DynamicRouter
402-
* You can add RouteFilterInterface instances to the NestedMatcher
403-
* The ``DynamicRouter`` or its components can be extended to allow modifications
404-
* You can implement your own Routers and add them to the ``ChainRouter``
405-
406-
.. note::
407-
408-
If you feel like your specific Enhancer or Router can be useful to others,
409-
get in touch with us and we'll try to include it in the bundle itself
410-
411394
Symfony2 integration
412395
--------------------
413396

414-
Like mentioned before, this bundle was designed to only require certain parts
415-
of Symfony2. However, if you wish to use it as part of your Symfony CMF project,
416-
an integration bundle is also available. We strongly recommend that you take
417-
a look at :doc:`../bundles/routing`.
418-
419-
For a starter's guide to the Routing bundle and its integration with Symfony2,
420-
refer to :doc:`../getting_started/routing`
421-
422-
We strongly recommend reading Symfony2's `Routing`_ component documentation
423-
page, as it's the base of this bundle's implementation.
397+
As mentioned before, this component was designed to use independently of the
398+
Symfony2 framework. However, if you wish to use it as part of your Symfony
399+
CMF project, an integration bundle is also available. Read more about the
400+
RoutingBundle in ":doc:`../bundles/routing`" in the bundles documentation.
424401

425402
.. _`Install it via Composer`: http://symfony.com/doc/current/components/using_components.html
426403
.. _`Packagist`: https://packagist.org/packages/symfony-cmf/routing

0 commit comments

Comments
 (0)