Earlier I was looking for an alternative to Symfony1′s routing.load_configuration event, so I could add some extra routes on-the-fly. This may be useful, when routes change in more ways than only variable request parameters as part of routes do (you know, like /blog/{id}). I got it completely wrong in my previous post about this subject. Of course, adding extra routes is a matter of creating a custom routing loader, and tell the framework about it using the service container. So, there we go.
First we have to create a custom route loader. This class should implement LoaderInterface, which is part of Symfony2′s Config component. It has two methods that are relevant here: supports() and load(). The loader resolver will ask our custom route loader if it can handle a resource (like “@AcmeDemoBundle/Controller/DemoController.php”) of a certain type (like “annotation”). We return true, if the type is “extra”. When this is the case, the resolver will call the load() method. It’s $resource element is irrelevant, since we want to add our extra routes anyway. The load() method should return a RouteCollection containing the new routes we want to add.
namespace Acme\RoutingBundle\Routing;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class ExtraLoader implements LoaderInterface
{
private $loaded = false;
public function load($resource, $type = null)
{
if (true === $this->loaded) {
throw new \RuntimeException('Do not add this loader twice');
}
$routes = new RouteCollection();
$pattern = '/extra';
$defaults = array(
'_controller' => 'AcmeRoutingBundle:Demo:extraRoute',
);
$route = new Route($pattern, $defaults);
$routes->add('extraRoute', $route);
return $routes;
}
public function supports($resource, $type = null)
{
return 'extra' === $type;
}
public function getResolver()
{
}
public function setResolver(LoaderResolver $resolver)
{
// irrelevant to us, since we don't need a resolver
}
}
Note: make sure the controller you specify really exists.
Now we make a service for our ExtraLoader.
<!-- in /src/Acme/RoutingBundle/Resources/config/services.xml -->
<?xml version="1.0" ?>
<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="acme.routing_loader" class="Acme\RoutingBundle\Routing\ExtraLoader">
<tag name="routing.loader"></tag>
</service>
</services>
</container>
Notice the tag called “routing.loader”. The delegating routing loader which is used by the framework, will look for all services with the tag “routing.loader” and add them as potential loaders. As said before, when one of these loaders gets a call to supports() and returns true, the load() method will be called and the loader is allowed to add some routes.
The last thing we need, is a few extra lines in /app/config/routing.yml:
AcmeRoutingBundle:
resource: .
type: extra
The “resource” key is irrelevant, but required. The important part here is “extra”. This is the “type” which our ExtraLoader supports and thus a call to it’s load() method will definitely be made.
Oh, don’t forget to clear the cache!

# January 6, 2012 at 14:00
Reply
# January 6, 2012 at 16:02
Reply
# February 28, 2013 at 08:16
Reply
# March 5, 2013 at 20:08
Reply
# January 7, 2012 at 11:29
Reply
# January 7, 2012 at 14:37
Reply
# February 9, 2012 at 09:14
Reply
# January 26, 2012 at 00:57
Reply
Pingback: A week of symfony #262 (2->8 January 2012) « We are php
# February 26, 2012 at 05:53
Reply
# February 26, 2012 at 10:43
Reply
# February 26, 2012 at 11:39
Reply
# February 28, 2012 at 10:16
Reply
# May 4, 2012 at 10:43
Reply
# June 22, 2012 at 08:48
Reply
# June 26, 2012 at 13:07
Reply
# July 13, 2012 at 15:32
Reply
# September 3, 2012 at 14:43
Reply
# September 3, 2012 at 15:48
Reply
# September 3, 2012 at 17:55
Reply
# September 4, 2012 at 08:11
Reply
# March 10, 2013 at 17:13
Reply
# March 11, 2013 at 20:30
Reply
# March 12, 2013 at 07:35
# March 22, 2013 at 20:17
Reply
# September 17, 2012 at 07:37
Reply
# September 17, 2012 at 07:44
Reply
# September 17, 2012 at 08:57
Reply
# September 27, 2012 at 11:04
Reply
# September 29, 2012 at 19:58
Reply
# October 11, 2012 at 21:09
Reply
# October 14, 2012 at 10:58
Reply
# October 25, 2012 at 02:04
Reply
# October 28, 2012 at 10:24
Reply
# October 25, 2012 at 15:13
Reply
# October 25, 2012 at 15:40
Reply
# October 26, 2012 at 08:19
Reply
# October 28, 2012 at 10:27
Reply
# November 24, 2012 at 13:54
Reply
# November 26, 2012 at 09:26
Reply
# January 19, 2013 at 21:41
Reply
# January 20, 2013 at 19:59
Reply
# March 13, 2013 at 20:39
Reply
# March 24, 2013 at 11:13
Reply
# March 24, 2013 at 16:14
Reply
# March 25, 2013 at 11:11
Reply
# March 24, 2013 at 23:45
Reply
# March 26, 2013 at 22:09
Reply
# March 30, 2013 at 11:21
Reply