Symfony Raindrop RoutingBundle

4.0.1 2019-07-23 20:21 UTC


Build Status

This bundle is based on routing extra bundle for symfony-cmf (available at https://github.com/symfony-cmf/RoutingBundle). If you want to use PHPCR ODM, the symfony cmf routing extra bundle features more routing options. This is the ORM simplified version.

It replaces symfony router with a chain router, appends standard router and dynamic router to it (as specified into configuration). This allows you to save routes to database and continue using symfony standard routing.

All routers attached to chain router can be sorted using a priority key specified into configuration.

This bundle also offers the option to link the route to any other entity, this object will be served when controller method gets invoked, as parameter.


First add the dependency to your composer.json file:

"require": {
    "raindrop/routing-bundle": "dev-master"

Then install the bundle with the command:

php composer.phar update

Enable the bundle in your application kernel:

// app/AppKernel.php

public function registerBundles()
    $bundles = array(
        // ...
        new Raindrop\RoutingBundle\RaindropRoutingBundle(),

Now the bundle is enabled.


Add following lines to app/config/config.yml:

      json: Sonata\Doctrine\Types\JsonType


      router.default: 100
      raindrop_routing.dynamic_router: 10
    replace_symfony_router: true

This will instruct the configuration to detach symfony router, attach chain router and append symfony standard router and the dynamic one.

The dynamic router fetches and stores routes from/to database using Doctrine ORM.


Bind routes to controllers:

$route = new Route;

A get request with /path/to/my/route will now point to AcmeDemoBundle:Default:index

Into twig templates:

{{ url('my_route') }}

This will resolve name to url as defined into database.

Bind routes to controller as service:

$route = new Route;

Bind routes to controller with target object(s):

In this case the controller must implement method signature as following. The parameter name 'content' is mandatory.

class myController {

    public function indexAction($content) 
        // your code here

Content will be passed to the controller when invoked.

$route = new Route;

When the reference field is 'id', it can be omitted as following.


This will retrieve Acme\DemoBundle\Entity\SampleEntity entity record with id 1.

You can also reference objects using other database fields:


Or use the setter method:

$route->setContent($object); // sets routeContent to 'Path\To\Entity:id:1'

$route->setContent($object, 'entity_id'); // sets routeContent 'Path\To\Entity:entity_id:1'

To bind a collection use the following syntax:


All Acme\DemoBundle\Entity\Route with locale 'en' will be returned.

To use the setter pass an array and the other parameters:

$route->setContent($array, $field, $value);

$value parameter can be implicit: $route->setContent($array, $field);. It will be retrieved from first array element, as well as class type.

Bind routes to other routes:

To obtain this, point the redirecting route to the target one and set the redirection controller (or another one that does the job). Optionally set 301 status code (default is 302) with 'permanent' property.

$route = new Route;
$route->setPermanent(); // this for 301 status


$route->setContent($anotherRoute); // instead of $route->setRouteContent('…');

Bind routes to external uri:

The target entity must implement Raindrop\RoutingBundle\Routing\Base\ExternalRouteInterface methods.

$externalRoute = new ExternalRoute();
$externalRoute->setPermanent(); // this is for 301 status

$route = new Route;


Routes path have a maximum length of 255 characters.