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

Commit e24c63f

Browse files
pcabreusdbu
authored andcommitted
ORM routes and routing auto adapter
1 parent d89204c commit e24c63f

File tree

7 files changed

+268
-0
lines changed

7 files changed

+268
-0
lines changed

book/routing.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ handle ``StaticContent`` as :ref:`explained above <start-routing-getting-control
520520
The PHPCR-ODM routes support more things, for example route parameters,
521521
requirements and defaults. This is explained in the
522522
:ref:`route document section in the RoutingBundle documentation <bundle-routing-document>`.
523+
You can also find :ref:`route entity documentation and Doctrine ORM integration <bundle-routing-entity>`.
523524

524525
Further Notes
525526
-------------

bundles/map.rst.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ library or they introduce a complete new concept.
6060
* :doc:`routing_auto/token_providers`
6161
* :doc:`routing_auto/conflict_resolvers`
6262
* :doc:`routing_auto/defunct_route_handlers`
63+
* :doc:`routing_auto/adapter`
64+
* :doc:`routing_auto/configuration`
6365

6466
* :doc:`search/index`
6567

bundles/routing/dynamic.rst

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,151 @@ the name indicates, loads ``Route`` entities from an ORM database.
408408
You must install the CoreBundle to use this feature if your application
409409
does not have at least DoctrineBundle 1.3.0.
410410

411+
.. _bundle-routing-route-entity:
412+
413+
The ORM Route entity
414+
--------------------
415+
416+
The example in this section applies if you use the ORM route provider
417+
(``Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\RouteProvider``). It uses the
418+
``staticPrefix`` field of the
419+
``Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route`` to find route candidates.
420+
421+
Symfony Cmf routing system allows us loading whatever content from a route.
422+
That means an entity route can reference to different types of entities.
423+
But Doctrine ORM is not able to establish that kind of mapping associations.
424+
To do that, the ORM RouteProvider follows the pattern of FQN:id. That is, the full
425+
model class name, then a colon, then the id. You only need to add it to the
426+
defaults parameters of the route with the ``RouteObjectInterface::CONTENT_ID``
427+
key. ``cmf_routing.content_repository`` service can help you to do it easily.
428+
A new route can be created in PHP code as follows::
429+
430+
// src/AppBundle/DataFixtures/ORM/LoadPostData.php
431+
namespace AppBundle\DataFixtures\ORM;
432+
433+
use AppBundle\Entity\Post;
434+
use Doctrine\Common\DataFixtures\FixtureInterface;
435+
use Doctrine\Common\Persistence\ObjectManager;
436+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route;
437+
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
438+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
439+
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
440+
441+
class LoadPostData implements FixtureInterface, ContainerAwareInterface
442+
{
443+
use ContainerAwareTrait;
444+
445+
/**
446+
* @param ObjectManager $manager
447+
*/
448+
public function load(ObjectManager $manager)
449+
{
450+
$post = new Post();
451+
$post->setTitle('My Content');
452+
$manager->persist($post);
453+
$manager->flush(); // flush to be able to use the generated id
454+
455+
$contentRepository = $this->container->get('cmf_routing.content_repository');
456+
457+
$route = new Route();
458+
$route->setName('my-content');
459+
$route->setStaticPrefix('/my-content');
460+
$route->setDefault(RouteObjectInterface::CONTENT_ID, $contentRepository->getContentId($post));
461+
$route->setContent($post);
462+
$post->addRoute($route); // Create the backlink from content to route
463+
464+
$manager->persist($post);
465+
$manager->flush();
466+
}
467+
}
468+
469+
Now the CMF will be able to handle requests for the URL ``/my-content``.
470+
471+
.. caution::
472+
473+
Make sure that the content already has an id before you set it on the route.
474+
The route to content link only works with single column ids.
475+
476+
The ``Post`` entity content in this example could be like this::
477+
478+
// src/AppBundle/Entity/Post.php
479+
namespace AppBundle\Entity;
480+
481+
use Doctrine\Common\Collections\ArrayCollection;
482+
use Doctrine\ORM\Mapping as ORM;
483+
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
484+
use Symfony\Cmf\Component\Routing\RouteReferrersInterface;
485+
486+
/**
487+
* @ORM\Table(name="post")
488+
* @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
489+
*/
490+
class Post implements RouteReferrersInterface
491+
{
492+
/** .. fields like title and body */
493+
494+
/**
495+
* @var RouteObjectInterface[]|ArrayCollection
496+
*
497+
* @ORM\ManyToMany(targetEntity="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route", cascade={"persist", "remove"})
498+
*/
499+
private $routes;
500+
501+
public function __construct()
502+
{
503+
$this->routes = new ArrayCollection();
504+
}
505+
506+
/**
507+
* @return RouteObjectInterface[]|ArrayCollection
508+
*/
509+
public function getRoutes()
510+
{
511+
return $this->routes;
512+
}
513+
514+
/**
515+
* @param RouteObjectInterface[]|ArrayCollection $routes
516+
*/
517+
public function setRoutes($routes)
518+
{
519+
$this->routes = $routes;
520+
}
521+
522+
/**
523+
* @param RouteObjectInterface $route
524+
*
525+
* @return $this
526+
*/
527+
public function addRoute($route)
528+
{
529+
$this->routes[] = $route;
530+
531+
return $this;
532+
}
533+
534+
/**
535+
* @param RouteObjectInterface $route
536+
*
537+
* @return $this
538+
*/
539+
public function removeRoute($route)
540+
{
541+
$this->routes->removeElement($route);
542+
543+
return $this;
544+
}
545+
}
546+
547+
Because you set the ``content_id`` default value on the route, the controller
548+
can expect the ``$contentDocument`` parameter. You can now configure which
549+
controller should handle ``Post`` entities as explained in the
550+
:ref:`Routing documentation <start-routing-getting-controller-template>`.
551+
552+
The ORM routes support more things, for example route parameters, requirements
553+
and defaults. This is explained in the
554+
:ref:`route document section <bundle-routing-document>`.
555+
411556
.. _bundles-routing-dynamic-generator:
412557

413558
URL generation with the DynamicRouter

bundles/routing_auto/adapter.rst

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
.. index::
2+
single: Adapter; RoutingAutoBundle
3+
4+
Adapter
5+
=======
6+
7+
Adapters abstract persistence operations. The ``PhpcrOdmAdapter`` is available
8+
for the Doctrine PHPCR-ODM persistence layer. If you want to use a different
9+
persistence layer, you need to implement your own adapter.
10+
11+
Implementing a custom adapter
12+
-----------------------------
13+
14+
Adapters have to implement ``Symfony\Cmf\Component\RoutingAuto\AdapterInterface``::
15+
16+
// src/AppBundle/Adapter/CustomAdapter.php
17+
namespace AppBundle\Adapter;
18+
19+
use Symfony\Cmf\Component\RoutingAuto\AdapterInterface;
20+
21+
class OrmAdapter implements AdapterInterface
22+
{
23+
// ... Implement all methods defined by the interface
24+
}
25+
26+
Adapters need to be registered as services and tagged with
27+
``cmf_routing_auto.adapter`` and an ``alias`` to identify the adapter:
28+
29+
.. configuration-block::
30+
31+
.. code-block:: yaml
32+
33+
services:
34+
app.custom_adapter:
35+
class: AppBundle\RoutingAuto\CustomAdapter
36+
tags:
37+
- { name: cmf_routing_auto.adapter, alias: "custom_adapter"}
38+
39+
.. code-block:: xml
40+
41+
<?xml version="1.0" encoding="UTF-8" ?>
42+
<container xmlns="http://symfony.com/schema/dic/services">
43+
<service
44+
id="app.custom_adapter"
45+
class="AppBundle\RoutingAuto\CustomAdapter"
46+
>
47+
<tag name="cmf_routing_auto.adapter" alias="custom_adapter"/>
48+
</service>
49+
</container>
50+
51+
.. code-block:: php
52+
53+
use Symfony\Component\DependencyInjection\Definition;
54+
use AppBundle\RoutingAuto\CustomAdapter;
55+
56+
$definition = new Definition(CustomAdapter::class);
57+
$definition->addTag('cmf_routing_auto.adapter', array('alias' => 'custom_adapter'));
58+
59+
$container->setDefinition('app.custom_adapter', $definition);
60+
61+
To use the new adapter, you specify the alias in the routing auto configuration:
62+
63+
.. configuration-block::
64+
65+
.. code-block:: yaml
66+
67+
# app/config/config.yml
68+
cmf_routing_auto:
69+
adapter: custom_adapter
70+
71+
.. code-block:: xml
72+
73+
<!-- app/config/config.xml -->
74+
<?xml version="1.0" encoding="UTF-8" ?>
75+
<container xmlns="http://symfony.com/schema/dic/services">
76+
<config xmlns="http://cmf.symfony.com/schema/dic/routingauto"
77+
adapter="custom_adapter"
78+
/>
79+
</container>
80+
81+
.. code-block:: php
82+
83+
$container->loadFromExtension('cmf_routing_auto', array(
84+
'adapter' => 'custom_adapter',
85+
));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Configuration Reference
2+
=======================
3+
4+
The RoutingAutoBundle can be configured under the ``cmf_routing_auto`` key in your
5+
application configuration.
6+
7+
Configuration
8+
-------------
9+
10+
``adapter``
11+
~~~~~~~~~~~
12+
13+
**type**: ``scalar`` **default**: ``doctrine_phpcr_odm`` if ``persistence`` configuration option is set ``phpcr``
14+
15+
This defines the adapter used to manage routes.
16+
17+
``auto_mapping``
18+
~~~~~~~~~~~~~~~~
19+
20+
**type**: ``boolean`` **default**: ``true``
21+
22+
Look for the configuration file ``cmf_routing_auto.yml`` in `Resource/config` folder of all
23+
available bundles.
24+
25+
``persistence``
26+
~~~~~~~~~~~~~~~
27+
28+
``phpcr``
29+
.........
30+
31+
.. todo

bundles/routing_auto/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ RoutingAutoBundle
99
conflict_resolvers
1010
defunct_route_handlers
1111
definitions
12+
adapter
13+
configuration

bundles/routing_auto/introduction.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ Read more
191191
* :doc:`conflict_resolvers`
192192
* :doc:`defunct_route_handlers`
193193
* :doc:`definitions`
194+
* :doc:`adapter`
195+
* :doc:`configuration`
194196

195197
.. _`with composer`: https://getcomposer.org/
196198
.. _`symfony-cmf/routing-auto-bundle`: https:/packagist.org/packages/symfony-cmf/routing-auto-bundle

0 commit comments

Comments
 (0)