|
| 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. |
0 commit comments