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

Commit a670204

Browse files
committed
Merge pull request #42 from dantleech/creating-a-custom-route-repository
Initial version
2 parents e3276c8 + a63cc71 commit a670204

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
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

@@ -298,3 +302,8 @@ extend it. You can also write your own routers to hook into the chain.
298302
.. _`CMF sandbox`: https://github.com/symfony-cmf/cmf-sandbox
299303
.. _`CMF Routing component`: https://github.com/symfony-cmf/Routing
300304
.. _`PHPCR-ODM`: https://github.com/doctrine/phpcr-odm
305+
306+
Learn more from the Cookbook
307+
----------------------------
308+
309+
* :doc:`../cookbook/using-a-custom-route-repository`
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
Using a custom route repository with Dynmaic Router
2+
===================================================
3+
4+
The Dynamic Router allows you to customize the route Repository (i.e. the class
5+
responsible for retrieving routes from the database), and by extension, the
6+
Route objects.
7+
8+
Creating the route repository
9+
-----------------------------
10+
11+
The route repository must implement the `RouteRepositoryInterface` The
12+
following class provides a simple solution using an ODM Repository.
13+
14+
.. code-block:: php
15+
16+
<?php
17+
18+
namespace MyVendor\Bundle\MyBundle\Repository;
19+
use Doctrine\ODM\PHPCR\DocumentRepository;
20+
use Symfony\Cmf\Component\Routing\RouteRepositoryInterface;
21+
use Symfony\Component\Routing\RouteCollection;
22+
use Symfony\Component\Routing\Route as SymfonyRoute;
23+
24+
class RouteRepository extends DocumentRepository implements RouteRepositoryInterface
25+
26+
{
27+
// this method is used to find routes matching the given URL
28+
public function findManyByUrl($url)
29+
{
30+
// for simplicity we retrieve one route
31+
$myDocument = $this->findOneBy(array(
32+
'url' => $url,
33+
));
34+
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+
));
44+
45+
// add the route to the RouteCollection using
46+
// a unique ID as the key.
47+
$collection->add('my_route_'.uniqid(), $route);
48+
49+
return $collection;
50+
}
51+
52+
// this method is used to generate URLs, e.g. {{ path('foobar') }}
53+
public function getRouteByName($name, $params = array())
54+
{
55+
$document = $this->findOneBy(array(
56+
'name' => $name,
57+
));
58+
59+
if ($route) {
60+
$route = new SymfonyRoute($route->getPattern(), array(
61+
'document' => $document,
62+
));
63+
}
64+
65+
return $route;
66+
}
67+
}
68+
69+
.. tip::
70+
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`.
76+
77+
Replacing the default CMF repository
78+
------------------------------------
79+
80+
To replace the default `RouteRepository` it is necessary to modify your configuration
81+
as follows:
82+
83+
.. configuration-block::
84+
85+
.. code-block:: yaml
86+
87+
# app/config/config.yml
88+
symfony_cmf_routing_extra:
89+
dynamic:
90+
enabled: true
91+
route_repository_service_id: my_bundle.repository.endpoint
92+
93+
Where `my_bundle.repository.endpoint` is the service ID of your repository.
94+
See `Creating and configuring services in the container <http://symfony.com/doc/current/book/service_container.html#creating-configuring-services-in-the-container/>`_
95+
for information on creating custom services.

index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Special solutions for special needs that go beyond standard usage.
5555
:maxdepth: 1
5656

5757
cookbook/phpcr-odm-custom-documentclass-mapper
58+
cookbook/using-a-custom-route-repository
5859

5960
Components
6061
----------

0 commit comments

Comments
 (0)