From df0343399b0dbc1e9880169c27ba73d106c978d0 Mon Sep 17 00:00:00 2001 From: dantleech Date: Sun, 18 Nov 2012 17:14:55 +0100 Subject: [PATCH 1/3] Initial version - Have yet to fill out the example of a custom route, as I havn't got round to this myself yet. --- cookbook/using-a-custom-route-repository.rst | 79 ++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 cookbook/using-a-custom-route-repository.rst diff --git a/cookbook/using-a-custom-route-repository.rst b/cookbook/using-a-custom-route-repository.rst new file mode 100644 index 00000000..2a4f3bc2 --- /dev/null +++ b/cookbook/using-a-custom-route-repository.rst @@ -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 + + 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: + + +.. code-block:: php + + `_ for information on creating custom services. From 9eb0a466c396eee14b8ff51d9004796f44c02c1a Mon Sep 17 00:00:00 2001 From: dantleech Date: Wed, 21 Nov 2012 09:45:43 +0100 Subject: [PATCH 2/3] Updated style and contents --- cookbook/using-a-custom-route-repository.rst | 35 +++++++++++++++----- index.rst | 1 + 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/cookbook/using-a-custom-route-repository.rst b/cookbook/using-a-custom-route-repository.rst index 2a4f3bc2..02848867 100644 --- a/cookbook/using-a-custom-route-repository.rst +++ b/cookbook/using-a-custom-route-repository.rst @@ -1,12 +1,20 @@ 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. +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. +The route repository must implement the `RouteRepositoryInterface` and in +addition should return instances of the Symfony Route class. The +following class provides a simple solution using an ODM Repository. For the +purpose of example we create the Symfony Route object and map the pattern +to it directly. + +Why return multiple routes? .. @todo explain that here. .. code-block:: php @@ -16,15 +24,20 @@ The route repository must implement the `RouteRepositoryInterface` and in additi 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 { public function findManyByUrl($url) { - $route = $this->findOneBy(array( + $myDocument = $this->findOneBy(array( 'path' => $url, )); + + $pattern = $myDocument->getUrl(); // e.g. "/this/is/a/route" + + $route = new SymfonyRoute($pattern); $collection = new RouteCollection(array($route)); return $collection; @@ -43,8 +56,9 @@ The route repository must implement the `RouteRepositoryInterface` and in additi 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: - +As noted above, the route classes provided by the route repository must +be instances of `Symfony\Component\Routing\Route`. A good pattern is to +have your ODM/ORM/Other Document extend the Symfony Route class. .. code-block:: php @@ -53,12 +67,12 @@ As noted above, the route classes provided by the route repository must *extend 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; + use Symfony\Component\Routing\Route as SymfonyRoute; /** * @PHPCR\Document(repositoryClass="MyVendor\Bundle\MyBundle\Repository\RouteRepository") */ - class Route extends BaseRoute + class Route extends SymfonyRoute { // @todo: Fill this out } @@ -66,7 +80,8 @@ As noted above, the route classes provided by the route repository must *extend 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: +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 @@ -76,4 +91,6 @@ The final step is to replace the default CMF routing repository service with you 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 `_ for information on creating custom services. +Where `my_bundle.repository.endpoint` is the service ID of your repository. +See `Creating and configuring services in the container `_ +for information on creating custom services. diff --git a/index.rst b/index.rst index 252d7fce..cc0c0668 100644 --- a/index.rst +++ b/index.rst @@ -55,6 +55,7 @@ Special solutions for special needs that go beyond standard usage. :maxdepth: 1 cookbook/phpcr-odm-custom-documentclass-mapper + cookbook/using-a-custom-route-repository Components ---------- From a63cc714af35431d74260a96c00cf0e46fb2788c Mon Sep 17 00:00:00 2001 From: dantleech Date: Sat, 24 Nov 2012 18:28:15 +0100 Subject: [PATCH 3/3] Updated cookbook, added link to routing-extra --- bundles/routing-extra.rst | 11 ++- cookbook/using-a-custom-route-repository.rst | 83 ++++++++++---------- 2 files changed, 51 insertions(+), 43 deletions(-) diff --git a/bundles/routing-extra.rst b/bundles/routing-extra.rst index 3bbc98f5..5193f338 100644 --- a/bundles/routing-extra.rst +++ b/bundles/routing-extra.rst @@ -204,12 +204,16 @@ The possible mappings are (in order of precedence): manager_registry: doctrine_phpcr manager_name: default - # if you use the default doctrine route repository servie, you can use this to customize + # if you use the default doctrine route repository service, you can use this to customize # the root path for the `PHPCR-ODM`_ RouteRepository # this base path will be injected by the Listener\IdPrefix - but only to routes # matching the prefix, to allow for more than one route source. routing_repositoryroot: /cms/routes + # If you want to replace the default route or content reposititories + # you can specify their service IDs here. + route_repository_service_id: my_bundle.repository.endpoint + content_repository_service_id: my_bundle.repository.endpoint To see some examples, please look at the `CMF sandbox`_ and specifically the routing fixtures loading. @@ -297,3 +301,8 @@ extend it. You can also write your own routers to hook into the chain. .. _`CMF sandbox`: https://github.com/symfony-cmf/cmf-sandbox .. _`CMF Routing component`: https://github.com/symfony-cmf/Routing .. _`PHPCR-ODM`: https://github.com/doctrine/phpcr-odm + +Learn more from the Cookbook +---------------------------- + +* :doc:`../cookbook/using-a-custom-route-repository` diff --git a/cookbook/using-a-custom-route-repository.rst b/cookbook/using-a-custom-route-repository.rst index 02848867..73f47915 100644 --- a/cookbook/using-a-custom-route-repository.rst +++ b/cookbook/using-a-custom-route-repository.rst @@ -8,13 +8,8 @@ Route objects. Creating the route repository ----------------------------- -The route repository must implement the `RouteRepositoryInterface` and in -addition should return instances of the Symfony Route class. The -following class provides a simple solution using an ODM Repository. For the -purpose of example we create the Symfony Route object and map the pattern -to it directly. - -Why return multiple routes? .. @todo explain that here. +The route repository must implement the `RouteRepositoryInterface` The +following class provides a simple solution using an ODM Repository. .. code-block:: php @@ -29,67 +24,71 @@ Why return multiple routes? .. @todo explain that here. 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( - 'path' => $url, + 'url' => $url, )); - $pattern = $myDocument->getUrl(); // e.g. "/this/is/a/route" + $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, + )); - $route = new SymfonyRoute($pattern); - $collection = new RouteCollection(array($route)); + // 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()) { - $route = $this->findOneBy(array( + $document = $this->findOneBy(array( 'name' => $name, )); + if ($route) { + $route = new SymfonyRoute($route->getPattern(), array( + 'document' => $document, + )); + } + return $route; } } -The route class ---------------- - -As noted above, the route classes provided by the route repository must -be instances of `Symfony\Component\Routing\Route`. A good pattern is to -have your ODM/ORM/Other Document extend the Symfony Route class. +.. tip:: -.. code-block:: php - - `_