-
Notifications
You must be signed in to change notification settings - Fork 156
Initial version #42
Initial version #42
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going to suggest changing it to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please have . instead of , before the "the following is an example..." There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
There was a problem hiding this comment.
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.