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

Commit a63cc71

Browse files
author
dantleech
committed
Updated cookbook, added link to routing-extra
1 parent 9eb0a46 commit a63cc71

File tree

2 files changed

+51
-43
lines changed

2 files changed

+51
-43
lines changed

bundles/routing-extra.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,16 @@ The possible mappings are (in order of precedence):
204204
manager_registry: doctrine_phpcr
205205
manager_name: default
206206
207-
# if you use the default doctrine route repository servie, you can use this to customize
207+
# if you use the default doctrine route repository service, you can use this to customize
208208
# the root path for the `PHPCR-ODM`_ RouteRepository
209209
# this base path will be injected by the Listener\IdPrefix - but only to routes
210210
# matching the prefix, to allow for more than one route source.
211211
routing_repositoryroot: /cms/routes
212212
213+
# If you want to replace the default route or content reposititories
214+
# you can specify their service IDs here.
215+
route_repository_service_id: my_bundle.repository.endpoint
216+
content_repository_service_id: my_bundle.repository.endpoint
213217
214218
To see some examples, please look at the `CMF sandbox`_ and specifically the routing fixtures loading.
215219

@@ -297,3 +301,8 @@ extend it. You can also write your own routers to hook into the chain.
297301
.. _`CMF sandbox`: https://github.com/symfony-cmf/cmf-sandbox
298302
.. _`CMF Routing component`: https://github.com/symfony-cmf/Routing
299303
.. _`PHPCR-ODM`: https://github.com/doctrine/phpcr-odm
304+
305+
Learn more from the Cookbook
306+
----------------------------
307+
308+
* :doc:`../cookbook/using-a-custom-route-repository`

cookbook/using-a-custom-route-repository.rst

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@ Route objects.
88
Creating the route repository
99
-----------------------------
1010

11-
The route repository must implement the `RouteRepositoryInterface` and in
12-
addition should return instances of the Symfony Route class. The
13-
following class provides a simple solution using an ODM Repository. For the
14-
purpose of example we create the Symfony Route object and map the pattern
15-
to it directly.
16-
17-
Why return multiple routes? .. @todo explain that here.
11+
The route repository must implement the `RouteRepositoryInterface` The
12+
following class provides a simple solution using an ODM Repository.
1813

1914
.. code-block:: php
2015
@@ -29,67 +24,71 @@ Why return multiple routes? .. @todo explain that here.
2924
class RouteRepository extends DocumentRepository implements RouteRepositoryInterface
3025
3126
{
27+
// this method is used to find routes matching the given URL
3228
public function findManyByUrl($url)
3329
{
30+
// for simplicity we retrieve one route
3431
$myDocument = $this->findOneBy(array(
35-
'path' => $url,
32+
'url' => $url,
3633
));
3734
38-
$pattern = $myDocument->getUrl(); // e.g. "/this/is/a/route"
35+
$pattern = $myDocument->getUrl(); // e.g. "/this/is/a/url"
36+
37+
$collection = new RouteCollection();
38+
39+
// create a new Route and set our document as
40+
// a default (so that we can retrieve it from the request)
41+
$route = new SymfonyRoute($ep->getPath(), array(
42+
'document' => $document,
43+
));
3944
40-
$route = new SymfonyRoute($pattern);
41-
$collection = new RouteCollection(array($route));
45+
// add the route to the RouteCollection using
46+
// a unique ID as the key.
47+
$collection->add('my_route_'.uniqid(), $route);
4248
4349
return $collection;
4450
}
4551
52+
// this method is used to generate URLs, e.g. {{ path('foobar') }}
4653
public function getRouteByName($name, $params = array())
4754
{
48-
$route = $this->findOneBy(array(
55+
$document = $this->findOneBy(array(
4956
'name' => $name,
5057
));
5158
59+
if ($route) {
60+
$route = new SymfonyRoute($route->getPattern(), array(
61+
'document' => $document,
62+
));
63+
}
64+
5265
return $route;
5366
}
5467
}
5568
56-
The route class
57-
---------------
58-
59-
As noted above, the route classes provided by the route repository must
60-
be instances of `Symfony\Component\Routing\Route`. A good pattern is to
61-
have your ODM/ORM/Other Document extend the Symfony Route class.
69+
.. tip::
6270

63-
.. code-block:: php
64-
65-
<?php
66-
67-
namespace MyVendor\Bundle\MyBundle\Document;
68-
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
69-
use DCMS\Bundle\CoreBundle\Validation\Constraints as RoutingValidation;
70-
use Symfony\Component\Routing\Route as SymfonyRoute;
71-
72-
/**
73-
* @PHPCR\Document(repositoryClass="MyVendor\Bundle\MyBundle\Repository\RouteRepository")
74-
*/
75-
class Route extends SymfonyRoute
76-
{
77-
// @todo: Fill this out
78-
}
71+
As you may have noticed we return a `RouteCollection` object - why not return
72+
a single `Route`? The Dynamic Router allows us to return many *candidate* routes,
73+
in other words, routes that *might* match the incoming URL. This is important to
74+
enable the possibility of matching *dynamic* routes, `/page/{page_id}/edit` for example.
75+
In our example we match the given URL exactly and only ever return a single `Route`.
7976

8077
Replacing the default CMF repository
8178
------------------------------------
8279

83-
The final step is to replace the default CMF routing repository service with
84-
your own. This is easily accomplished using the application configuration:
80+
To replace the default `RouteRepository` it is necessary to modify your configuration
81+
as follows:
82+
83+
.. configuration-block::
8584

86-
.. code-block:: yaml
85+
.. code-block:: yaml
8786
88-
# app/config/config.yml
89-
symfony_cmf_routing_extra:
90-
dynamic:
91-
enabled: true
92-
route_repository_service_id: my_bundle.repository.endpoint
87+
# app/config/config.yml
88+
symfony_cmf_routing_extra:
89+
dynamic:
90+
enabled: true
91+
route_repository_service_id: my_bundle.repository.endpoint
9392
9493
Where `my_bundle.repository.endpoint` is the service ID of your repository.
9594
See `Creating and configuring services in the container <http://symfony.com/doc/current/book/service_container.html#creating-configuring-services-in-the-container/>`_

0 commit comments

Comments
 (0)