|
| 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. |
0 commit comments