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 2 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
54 changes: 52 additions & 2 deletions bundles/routing/dynamic_customize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,62 @@ 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
Copy link
Member

Choose a reason for hiding this comment

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

Let's show that one has to return the new defaults array like this:

public function enhance(array $defaults, Request $request)
{
    // ... tweak the $defaults array

    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 }
Copy link
Member

Choose a reason for hiding this comment

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

missing , before priority


.. 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;

$definitionSendmail = new Definition('AppBundle\Routing\Enhancer\SimpleEnhancer');
$definitionSendmail->addTag('dynamic_router_route_enhancer',array('priority' => 10));
Copy link
Member

Choose a reason for hiding this comment

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

the variable seems to be a copy-paste mistake. Let's rename it to simply $definition.

$container->setDefinition('app.routing.enhancer.simple', $definitionSendmail);

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

Expand Down