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

Initial version #42

Merged
merged 3 commits into from
Nov 25, 2012
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions cookbook/using-a-custom-route-repository.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
Using a custom route repository with Dynmaic Router
===================================================

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we try to keep lines broken after 80-100 characters. this makes it easier to read diffs and when somebody fixes a spelling error, you have to scan just one line of < 100 characters to see what changed, instead of the whole section. to rst its all the same, a new section is only started with a blank line.


Creating the route repository
-----------------------------

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.

.. code-block:: php

<?php

namespace MyVendor\Bundle\MyBundle\Repository;
use Doctrine\ODM\PHPCR\DocumentRepository;
use Symfony\Cmf\Component\Routing\RouteRepositoryInterface;
use Symfony\Component\Routing\RouteCollection;

class RouteRepository extends DocumentRepository implements RouteRepositoryInterface

{
public function findManyByUrl($url)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, really off topic for a doc PR, but what if we would change this to findManyByRequest and pass the whole request? i.e. symfony 2.2 supports hostnamePattern on routes, so we might want to know the hostname that was used in this request...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to suggest changing it to findByUrl so as to make it more sensible when only returning one route, but yeah, I think it makes sense to pass in the request. What about "getRouteCollectionForRequest" ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like that name. created a PR - feedback welcome: symfony-cmf/Routing#33

{
$route = $this->findOneBy(array(
'path' => $url,
));
$collection = new RouteCollection(array($route));

return $collection;
}

public function getRouteByName($name, $params = array())
{
$route = $this->findOneBy(array(
'name' => $name,
));

return $route;
}
}

The route class
---------------

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please have . instead of , before the "the following is an example..."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the route object may be of the Route class itself, not necessarily extend. if somebody does not use an OxM he can also just create new Route from symfony core. maybe we should state that here to make people not do overly complicated things.



.. code-block:: php

<?php

namespace MyVendor\Bundle\MyBundle\Document;
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
use DCMS\Bundle\CoreBundle\Validation\Constraints as RoutingValidation;
use Symfony\Component\Routing\Route as BaseRoute;

/**
* @PHPCR\Document(repositoryClass="MyVendor\Bundle\MyBundle\Repository\RouteRepository")
*/
class Route extends BaseRoute
{
// @todo: Fill this out
}

Replacing the default CMF repository
------------------------------------

The final step is to replace the default CMF routing repository service with your own. This is easily accomplished using the application configuration:

.. code-block:: yaml

# app/config/config.yml
symfony_cmf_routing_extra:
dynamic:
enabled: true
route_repository_service_id: my_bundle.repository.endpoint

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.