This repository was archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
Initial version #42
Merged
dbu
merged 3 commits into
symfony-cmf:master
from
dantleech:creating-a-custom-route-repository
Nov 25, 2012
Merged
Initial version #42
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
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` The | ||
following class provides a simple solution using an ODM Repository. | ||
|
||
.. 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; | ||
use Symfony\Component\Routing\Route as SymfonyRoute; | ||
|
||
class RouteRepository extends DocumentRepository implements RouteRepositoryInterface | ||
|
||
{ | ||
// this method is used to find routes matching the given URL | ||
public function findManyByUrl($url) | ||
{ | ||
// for simplicity we retrieve one route | ||
$myDocument = $this->findOneBy(array( | ||
'url' => $url, | ||
)); | ||
|
||
$pattern = $myDocument->getUrl(); // e.g. "/this/is/a/url" | ||
|
||
$collection = new RouteCollection(); | ||
|
||
// create a new Route and set our document as | ||
// a default (so that we can retrieve it from the request) | ||
$route = new SymfonyRoute($ep->getPath(), array( | ||
'document' => $document, | ||
)); | ||
|
||
// add the route to the RouteCollection using | ||
// a unique ID as the key. | ||
$collection->add('my_route_'.uniqid(), $route); | ||
|
||
return $collection; | ||
} | ||
|
||
// this method is used to generate URLs, e.g. {{ path('foobar') }} | ||
public function getRouteByName($name, $params = array()) | ||
{ | ||
$document = $this->findOneBy(array( | ||
'name' => $name, | ||
)); | ||
|
||
if ($route) { | ||
$route = new SymfonyRoute($route->getPattern(), array( | ||
'document' => $document, | ||
)); | ||
} | ||
|
||
return $route; | ||
} | ||
} | ||
|
||
.. tip:: | ||
|
||
As you may have noticed we return a `RouteCollection` object - why not return | ||
a single `Route`? The Dynamic Router allows us to return many *candidate* routes, | ||
in other words, routes that *might* match the incoming URL. This is important to | ||
enable the possibility of matching *dynamic* routes, `/page/{page_id}/edit` for example. | ||
In our example we match the given URL exactly and only ever return a single `Route`. | ||
|
||
Replacing the default CMF repository | ||
------------------------------------ | ||
|
||
To replace the default `RouteRepository` it is necessary to modify your configuration | ||
as follows: | ||
|
||
.. configuration-block:: | ||
|
||
.. 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 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" ?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.
like that name. created a PR - feedback welcome: symfony-cmf/Routing#33