@@ -8,13 +8,8 @@ Route objects.
8
8
Creating the route repository
9
9
-----------------------------
10
10
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.
18
13
19
14
.. code-block :: php
20
15
@@ -29,67 +24,71 @@ Why return multiple routes? .. @todo explain that here.
29
24
class RouteRepository extends DocumentRepository implements RouteRepositoryInterface
30
25
31
26
{
27
+ // this method is used to find routes matching the given URL
32
28
public function findManyByUrl($url)
33
29
{
30
+ // for simplicity we retrieve one route
34
31
$myDocument = $this->findOneBy(array(
35
- 'path ' => $url,
32
+ 'url ' => $url,
36
33
));
37
34
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
+ ));
39
44
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);
42
48
43
49
return $collection;
44
50
}
45
51
52
+ // this method is used to generate URLs, e.g. {{ path('foobar') }}
46
53
public function getRouteByName($name, $params = array())
47
54
{
48
- $route = $this->findOneBy(array(
55
+ $document = $this->findOneBy(array(
49
56
'name' => $name,
50
57
));
51
58
59
+ if ($route) {
60
+ $route = new SymfonyRoute($route->getPattern(), array(
61
+ 'document' => $document,
62
+ ));
63
+ }
64
+
52
65
return $route;
53
66
}
54
67
}
55
68
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 ::
62
70
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 `.
79
76
80
77
Replacing the default CMF repository
81
78
------------------------------------
82
79
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 ::
85
84
86
- .. code-block :: yaml
85
+ .. code-block :: yaml
87
86
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
93
92
94
93
Where `my_bundle.repository.endpoint ` is the service ID of your repository.
95
94
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