|
| 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 responsible for retrieving routes from the database), and by extension, the Route objects. |
| 5 | + |
| 6 | +Creating the route repository |
| 7 | +----------------------------- |
| 8 | + |
| 9 | +The route repository must implement the `RouteRepositoryInterface` and in addition should return objects which extend the Symfony Route class. The following class provides a simple solution which uses an ODM Repository, but you can equally imagine an ORM repository or indeed anything you like, as long as it implements the interface. |
| 10 | + |
| 11 | +.. code-block:: php |
| 12 | +
|
| 13 | + <?php |
| 14 | +
|
| 15 | + namespace MyVendor\Bundle\MyBundle\Repository; |
| 16 | + use Doctrine\ODM\PHPCR\DocumentRepository; |
| 17 | + use Symfony\Cmf\Component\Routing\RouteRepositoryInterface; |
| 18 | + use Symfony\Component\Routing\RouteCollection; |
| 19 | +
|
| 20 | + class RouteRepository extends DocumentRepository implements RouteRepositoryInterface |
| 21 | +
|
| 22 | + { |
| 23 | + public function findManyByUrl($url) |
| 24 | + { |
| 25 | + $route = $this->findOneBy(array( |
| 26 | + 'path' => $url, |
| 27 | + )); |
| 28 | + $collection = new RouteCollection(array($route)); |
| 29 | +
|
| 30 | + return $collection; |
| 31 | + } |
| 32 | +
|
| 33 | + public function getRouteByName($name, $params = array()) |
| 34 | + { |
| 35 | + $route = $this->findOneBy(array( |
| 36 | + 'name' => $name, |
| 37 | + )); |
| 38 | +
|
| 39 | + return $route; |
| 40 | + } |
| 41 | + } |
| 42 | +
|
| 43 | +The route class |
| 44 | +--------------- |
| 45 | + |
| 46 | +As noted above, the route classes provided by the route repository must *extend* `Symfony\Component\Routing\Route` and provide whatever other parameters required by the storage engine, the following is an example ODM object: |
| 47 | + |
| 48 | + |
| 49 | +.. code-block:: php |
| 50 | +
|
| 51 | + <?php |
| 52 | +
|
| 53 | + namespace MyVendor\Bundle\MyBundle\Document; |
| 54 | + use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR; |
| 55 | + use DCMS\Bundle\CoreBundle\Validation\Constraints as RoutingValidation; |
| 56 | + use Symfony\Component\Routing\Route as BaseRoute; |
| 57 | +
|
| 58 | + /** |
| 59 | + * @PHPCR\Document(repositoryClass="MyVendor\Bundle\MyBundle\Repository\RouteRepository") |
| 60 | + */ |
| 61 | + class Route extends BaseRoute |
| 62 | + { |
| 63 | + // @todo: Fill this out |
| 64 | + } |
| 65 | +
|
| 66 | +Replacing the default CMF repository |
| 67 | +------------------------------------ |
| 68 | + |
| 69 | +The final step is to replace the default CMF routing repository service with your own. This is easily accomplished using the application configuration: |
| 70 | + |
| 71 | +.. code-block:: yaml |
| 72 | +
|
| 73 | + # app/config/config.yml |
| 74 | + symfony_cmf_routing_extra: |
| 75 | + dynamic: |
| 76 | + enabled: true |
| 77 | + route_repository_service_id: my_bundle.repository.endpoint |
| 78 | + |
| 79 | +Where `my_bundle.repository.endpoint` is the service ID of your repository. See `Creating and configuring services in the container <http://symfony.com/doc/current/book/service_container.html#creating-configuring-services-in-the-container/>`_ for information on creating custom services. |
0 commit comments