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

add example Writing your own Route Enhancers #744

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions bundles/routing/dynamic_customize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,65 @@ Writing your own Route Enhancers

You can add your own :ref:`RouteEnhancerInterface <bundles-routing-dynamic_router-enhancer>`
implementations if you have a case not handled by the
:ref:`provided enhancers <component-routing-enhancers>`. Simply define services
for your enhancers and tag them with ``dynamic_router_route_enhancer`` to have
:ref:`provided enhancers <component-routing-enhancers>`.

.. code-block:: php

// src/AppBundle/Routing/Enhancer/SimpleEnhancer.php
namespace AppBundle\Routing\Enhancer;

use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface;
use Symfony\Component\HttpFoundation\Request;

class SimpleEnhancer implements RouteEnhancerInterface
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code would be invalid, we should add a method that does something trivial to make the example complete.

public function enhance(array $defaults, Request $request)
{
// own logic.

// Enhancer MUST return the $defaults but may add or remove values.
return $defaults;
}
}


Simply define services for your enhancers and tag them with ``dynamic_router_route_enhancer`` to have
them added to the routing. You can specify an optional ``priority`` parameter
on the tag to control the order in which enhancers are executed. The higher the
priority, the earlier the enhancer is executed.

.. configuration-block::

.. code-block:: yaml

services:
app.routing.enhancer.simple:
class: AppBundle\Routing\Enhancer\SimpleEnhancer
tags:
- { name: dynamic_router_route_enhancer, priority: 10 }

.. code-block:: xml

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="app.routing.enhancer.simple" class="AppBundle\Routing\Enhancer\SimpleEnhancer">
<tag name="dynamic_router_route_enhancer" priority="10" />
</service>
</services>
</container>

.. code-block:: php

use Symfony\Component\DependencyInjection\Definition;

$definition = new Definition('AppBundle\Routing\Enhancer\SimpleEnhancer');
$definition->addTag('dynamic_router_route_enhancer',array('priority' => 10));
$container->setDefinition('app.routing.enhancer.simple', $definition);

.. index:: Route Provider
.. _bundle-routing-custom_provider:

Expand Down