7
7
8
8
The Symfony CMF Routing component extends the Symfony2 core routing
9
9
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.
12
11
13
12
At the core of the Symfony CMF Routing component is the `ChainRouter `_. The
14
13
ChainRouter tries to match a request with each of its registered routers,
@@ -162,7 +161,7 @@ parameters from the information provided by the match.
162
161
.. _components-routing-events :
163
162
164
163
Events
165
- ~~~~~~
164
+ ......
166
165
167
166
Events are dispatched during the Dynamic Router match process. Currently there
168
167
are 2 events:
@@ -199,15 +198,14 @@ to use when handling the current Request:
199
198
""""""""""""""""""""
200
199
201
200
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
203
202
database, effectively allowing you to store and manage routes dynamically from
204
- database.
203
+ the database.
205
204
206
205
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.
211
209
212
210
The underlying implementation of the ``RouteProviderInterface `` is not in the
213
211
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
218
216
the methods::
219
217
220
218
use Symfony\Cmf\Component\Routing\RouteProviderInterface;
219
+
221
220
use Symfony\Component\Routing\RouteCollection;
222
221
use Symfony\Component\Routing\Exception\RouteNotFoundException;
223
222
@@ -269,7 +268,7 @@ The Route Provider is set using the first argument of the constructor for the
269
268
// ...
270
269
271
270
$routeProvider = new DoctrineOrmRouteProvider(...);
272
- $nestedMatcher = new NestedMatcher($routeProvider);
271
+ $nestedMatcher = new NestedMatcher($routeProvider, ... );
273
272
274
273
2. The Route Filters
275
274
""""""""""""""""""""
@@ -279,14 +278,32 @@ implementations to reduce the provided ``Route`` objects, e.g. for doing
279
278
content negotiation. It is the responsibility of each filter to throw the
280
279
``ResourceNotFoundException `` if no more routes are left in the collection.
281
280
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
+
282
286
3. The Final Matcher
283
287
""""""""""""""""""""
284
288
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
288
292
: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);
290
307
291
308
.. _component-routing-enhancers :
292
309
@@ -306,40 +323,43 @@ already provides some simple enhancers:
306
323
* `RouteContentEnhancer `_
307
324
308
325
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.
310
329
311
330
Linking a Route with a Content
312
331
..............................
313
332
314
333
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.
321
340
322
341
Furthermore, routes that implement this interface can also provide a custom
323
342
Route name. The key returned by ``getRouteKey `` will be used as route name
324
343
instead of the Symfony core compatible route name and can contain any
325
344
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 `` .
328
347
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.
330
351
331
352
Redirections
332
353
............
333
354
334
355
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.
338
358
339
359
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
341
361
implementing that redirection under the full Symfony2 stack, refer to
342
- :doc: `../bundles/routing `.
362
+ :doc: `the RoutingBundle < ../bundles/routing > `.
343
363
344
364
Generating URLs
345
365
~~~~~~~~~~~~~~~
@@ -349,20 +369,20 @@ is also responsible for generating an URL from a Route and its parameters.
349
369
The ``ChainRouter `` iterates over its known routers until one of them is
350
370
able to generate a matching URL.
351
371
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.
356
376
357
377
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.
366
386
Using the ``ContentAwareGenerator ``, you can generate urls for your content in
367
387
three ways:
368
388
@@ -371,56 +391,13 @@ three ways:
371
391
* Or provide an implementation of ``ContentRepositoryInterface `` and pass the id
372
392
of the content object as parameter ``content_id `` and ``null `` as $name.
373
393
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
-
411
394
Symfony2 integration
412
395
--------------------
413
396
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.
424
401
425
402
.. _`Install it via Composer` : http://symfony.com/doc/current/components/using_components.html
426
403
.. _`Packagist` : https://packagist.org/packages/symfony-cmf/routing
0 commit comments