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

Commit a408bc8

Browse files
committed
Merge remote-tracking branch 'origin/master' into 2.0
2 parents f7078fe + e24c63f commit a408bc8

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
@@ -46,6 +46,8 @@ library or they introduce a complete new concept.
4646
* :doc:`routing_auto/token_providers`
4747
* :doc:`routing_auto/conflict_resolvers`
4848
* :doc:`routing_auto/defunct_route_handlers`
49+
* :doc:`routing_auto/adapter`
50+
* :doc:`routing_auto/configuration`
4951

5052
* :doc:`seo/index`
5153

bundles/routing/dynamic.rst

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

339+
.. _bundle-routing-route-entity:
340+
341+
The ORM Route entity
342+
--------------------
343+
344+
The example in this section applies if you use the ORM route provider
345+
(``Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\RouteProvider``). It uses the
346+
``staticPrefix`` field of the
347+
``Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route`` to find route candidates.
348+
349+
Symfony Cmf routing system allows us loading whatever content from a route.
350+
That means an entity route can reference to different types of entities.
351+
But Doctrine ORM is not able to establish that kind of mapping associations.
352+
To do that, the ORM RouteProvider follows the pattern of FQN:id. That is, the full
353+
model class name, then a colon, then the id. You only need to add it to the
354+
defaults parameters of the route with the ``RouteObjectInterface::CONTENT_ID``
355+
key. ``cmf_routing.content_repository`` service can help you to do it easily.
356+
A new route can be created in PHP code as follows::
357+
358+
// src/AppBundle/DataFixtures/ORM/LoadPostData.php
359+
namespace AppBundle\DataFixtures\ORM;
360+
361+
use AppBundle\Entity\Post;
362+
use Doctrine\Common\DataFixtures\FixtureInterface;
363+
use Doctrine\Common\Persistence\ObjectManager;
364+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route;
365+
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
366+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
367+
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
368+
369+
class LoadPostData implements FixtureInterface, ContainerAwareInterface
370+
{
371+
use ContainerAwareTrait;
372+
373+
/**
374+
* @param ObjectManager $manager
375+
*/
376+
public function load(ObjectManager $manager)
377+
{
378+
$post = new Post();
379+
$post->setTitle('My Content');
380+
$manager->persist($post);
381+
$manager->flush(); // flush to be able to use the generated id
382+
383+
$contentRepository = $this->container->get('cmf_routing.content_repository');
384+
385+
$route = new Route();
386+
$route->setName('my-content');
387+
$route->setStaticPrefix('/my-content');
388+
$route->setDefault(RouteObjectInterface::CONTENT_ID, $contentRepository->getContentId($post));
389+
$route->setContent($post);
390+
$post->addRoute($route); // Create the backlink from content to route
391+
392+
$manager->persist($post);
393+
$manager->flush();
394+
}
395+
}
396+
397+
Now the CMF will be able to handle requests for the URL ``/my-content``.
398+
399+
.. caution::
400+
401+
Make sure that the content already has an id before you set it on the route.
402+
The route to content link only works with single column ids.
403+
404+
The ``Post`` entity content in this example could be like this::
405+
406+
// src/AppBundle/Entity/Post.php
407+
namespace AppBundle\Entity;
408+
409+
use Doctrine\Common\Collections\ArrayCollection;
410+
use Doctrine\ORM\Mapping as ORM;
411+
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
412+
use Symfony\Cmf\Component\Routing\RouteReferrersInterface;
413+
414+
/**
415+
* @ORM\Table(name="post")
416+
* @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
417+
*/
418+
class Post implements RouteReferrersInterface
419+
{
420+
/** .. fields like title and body */
421+
422+
/**
423+
* @var RouteObjectInterface[]|ArrayCollection
424+
*
425+
* @ORM\ManyToMany(targetEntity="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm\Route", cascade={"persist", "remove"})
426+
*/
427+
private $routes;
428+
429+
public function __construct()
430+
{
431+
$this->routes = new ArrayCollection();
432+
}
433+
434+
/**
435+
* @return RouteObjectInterface[]|ArrayCollection
436+
*/
437+
public function getRoutes()
438+
{
439+
return $this->routes;
440+
}
441+
442+
/**
443+
* @param RouteObjectInterface[]|ArrayCollection $routes
444+
*/
445+
public function setRoutes($routes)
446+
{
447+
$this->routes = $routes;
448+
}
449+
450+
/**
451+
* @param RouteObjectInterface $route
452+
*
453+
* @return $this
454+
*/
455+
public function addRoute($route)
456+
{
457+
$this->routes[] = $route;
458+
459+
return $this;
460+
}
461+
462+
/**
463+
* @param RouteObjectInterface $route
464+
*
465+
* @return $this
466+
*/
467+
public function removeRoute($route)
468+
{
469+
$this->routes->removeElement($route);
470+
471+
return $this;
472+
}
473+
}
474+
475+
Because you set the ``content_id`` default value on the route, the controller
476+
can expect the ``$contentDocument`` parameter. You can now configure which
477+
controller should handle ``Post`` entities as explained in the
478+
:ref:`Routing documentation <start-routing-getting-controller-template>`.
479+
480+
The ORM routes support more things, for example route parameters, requirements
481+
and defaults. This is explained in the
482+
:ref:`route document section <bundle-routing-document>`.
483+
339484
.. _bundles-routing-dynamic-generator:
340485

341486
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)