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

Commit 84f8829

Browse files
committed
Split introduction in seperate articles
1 parent 6592c97 commit 84f8829

File tree

5 files changed

+485
-352
lines changed

5 files changed

+485
-352
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
Customization
2+
-------------
3+
4+
.. _routingauto_customization_pathproviders:
5+
6+
Adding Path Providers
7+
~~~~~~~~~~~~~~~~~~~~~
8+
9+
The goal of a ``PathProvider`` class is to add one or several path elements to
10+
the route stack. For example, the following provider will add the path
11+
``foo/bar`` to the route stack::
12+
13+
<?php
14+
15+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\PathProviderInterface;
16+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
17+
18+
class FoobarProvider implements PathProviderInterface
19+
{
20+
public function providePath(RouteStack $routeStack)
21+
{
22+
$routeStack->addPathElements(array('foo', 'bar'));
23+
}
24+
}
25+
26+
To use the path provider you must register it in the **DIC** and add the
27+
``cmf_routing_auto.provider`` tag and set the **alias** accordingly.
28+
29+
.. configuration-block::
30+
31+
.. code-block:: yaml
32+
33+
my_cms.some_bundle.path_provider.foobar:
34+
class: "FoobarProvider"
35+
scope: prototype
36+
tags:
37+
- { name: cmf_routing_auto.provider, alias: "foobar"}
38+
39+
.. code-block:: xml
40+
41+
<service
42+
id="my_cms.some_bundle.path_provider.foobar"
43+
class="FoobarProvider"
44+
scope="prototype"
45+
>
46+
<tag name="cmf_routing_auto.provider" alias="foobar"/>
47+
</service>
48+
49+
.. code-block:: php
50+
51+
use Symfony\Component\DependencyInjection\Definition;
52+
53+
$definition = new Definition('FooBarProvider');
54+
$definition->addTag('cmf_routing_auto.provider', array('alias' => 'foobar'));
55+
$definition->setScope('prototype');
56+
57+
$container->setDefinition('my_cms.some_bundle.path_provider.foobar', $definition);
58+
59+
The **foobar** path provider is now available as **foobar**.
60+
61+
.. note::
62+
63+
The that both path providers and path actions need to be defined with a
64+
scope of "prototype". This ensures that each time the auto routing system
65+
requests the class a new one is given and we do not have any state
66+
problems.
67+
68+
Adding Path Actions
69+
~~~~~~~~~~~~~~~~~~~
70+
71+
In the auto routing system, a "path action" is an action to take if the path
72+
provided by the "path provider" exists or not.
73+
74+
You can add a path action by extending the ``PathActionInterface`` and
75+
registering your new class correctly in the DI configuration.
76+
77+
This is a very simple implementation from the bundle - it is used to throw an
78+
exception when a path already exists::
79+
80+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\PathNotExists;
81+
82+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\PathActionInterface;
83+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Exception\CouldNotFindRouteException;
84+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
85+
86+
class ThrowException implements PathActionInterface
87+
{
88+
public function init(array $options)
89+
{
90+
}
91+
92+
public function execute(RouteStack $routeStack)
93+
{
94+
throw new CouldNotFindRouteException('/'.$routeStack->getFullPath());
95+
}
96+
}
97+
98+
It is registered in the DI configuration as follows:
99+
100+
.. configuration-block::
101+
102+
.. code-block:: yaml
103+
104+
cmf_routing_auto.not_exists_action.throw_exception
105+
class: "My\Cms\AutoRoute\PathNotExists\ThrowException"
106+
scope: prototype
107+
tags:
108+
- { name: cmf_routing_auto.provider, alias: "throw_exception"}
109+
110+
.. code-block:: xml
111+
112+
<service
113+
id="my_cms.not_exists_action.throw_exception"
114+
class="My\Cms\AutoRoute\PathNotExists\ThrowException"
115+
scope="prototype"
116+
>
117+
<tag name="cmf_routing_auto.not_exists_action" alias="throw_exception"/>
118+
</service>
119+
120+
.. code-block:: php
121+
122+
use Symfony\Component\DependencyInjection\Definition;
123+
124+
$definition = new Definition('My\Cms\AutoRoute\PathNotExists\ThrowException');
125+
$definition->addTag('cmf_routing_auto.provider', array('alias' => 'throw_exception'));
126+
$definition->setScope('prototype');
127+
128+
$container->setDefinition('my_cms.some_bundle.path_provider.throw_exception', $definition);
129+
130+
Note the following:
131+
132+
* **Scope**: Must *always* be set to *prototype*;
133+
* **Tag**: The tag registers the service with the auto routing system, it can be one of the following;
134+
* ``cmf_routing_auto.exists.action`` - if the action is to be used when a path exists;
135+
* ``cmf_routing_auto.not_exists.action`` - if the action is to be used when a path does not exist;
136+
* **Alias**: The alias of the tag is the name by which you will reference this action in the auto routing schema.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
.. index::
2+
single: Exists Actions; RoutingAutoBundle
3+
4+
Path Exists Actions
5+
-------------------
6+
7+
These are the default actions available to take if the path provided by a
8+
`path_provider` already exists and so creating a new path would create a
9+
conflict.
10+
11+
auto_increment
12+
~~~~~~~~~~~~~~
13+
14+
The ``auto_increment`` action will add a numerical suffix to the path, for
15+
example ``my/path`` would first become ``my/path-1`` and if that path *also*
16+
exists it will try ``my/path-2``, ``my/path-3`` and so on into infinity until
17+
it finds a path which *doesn't* exist.
18+
19+
This action should typically be used in the ``content_name`` builder unit to
20+
resolve conflicts. Using it in the ``content_path`` builder chain would not
21+
make much sense (I can't imagine any use cases at the moment).
22+
23+
Example:
24+
25+
.. configuration-block::
26+
27+
.. code-block:: yaml
28+
29+
exists_action: auto_increment
30+
31+
.. code-block:: xml
32+
33+
<exists-action name="auto_increment" />
34+
35+
.. code-block:: php
36+
37+
array(
38+
// ...
39+
'exists_action' => 'auto_increment',
40+
);
41+
42+
use
43+
~~~
44+
45+
The ``use`` action will simply take the existing path and use it. For example,
46+
in our post example the first builder unit must first determine the blogs
47+
path, ``/my/blog``, if this path exists (and it should) then we will *use* it
48+
in the stack.
49+
50+
This action should typically be used in one of the content path builder units
51+
to specify that we should use the existing route, on the other hand, using
52+
this as the content name builder action should cause the old route to be
53+
overwritten.
54+
55+
Example:
56+
57+
.. configuration-block::
58+
59+
.. code-block:: yaml
60+
61+
exists_action: use
62+
63+
.. code-block:: xml
64+
65+
<exists-action name="use" />
66+
67+
.. code-block:: php
68+
69+
array(
70+
// ...
71+
'exists_action' => 'use',
72+
);

0 commit comments

Comments
 (0)