How to Create a custom Route Loader
Edit this pageWarning: You are browsing the documentation for Symfony 3.0, which is no longer maintained.
Read the updated version of this page for Symfony 6.3 (the current stable version).
How to Create a custom Route Loader
What is a Custom Route Loader
A custom route loader enables you to generate routes based on some conventions or patterns. A great example for this use-case is the FOSRestBundle where routes are generated based on the names of the action methods in a controller.
You still need to modify your routing configuration (e.g.
app/config/routing.yml
) manually, even when using a custom route
loader.
Note
There are many bundles out there that use their own route loaders to accomplish cases like those described above, for instance FOSRestBundle, JMSI18nRoutingBundle, KnpRadBundle and SonataAdminBundle.
Loading Routes
The routes in a Symfony application are loaded by the
DelegatingLoader.
This loader uses several other loaders (delegates) to load resources of
different types, for instance YAML files or @Route
and @Method
annotations
in controller files. The specialized loaders implement
LoaderInterface
and therefore have two important methods:
supports()
and load().
Take these lines from the routing.yml
in the Symfony Standard Edition:
1 2 3 4
# app/config/routing.yml
app:
resource: '@AppBundle/Controller/'
type: annotation
When the main loader parses this, it tries all registered delegate loaders and calls
their supports()
method with the given resource (@AppBundle/Controller/
)
and type (annotation
) as arguments. When one of the loader returns true
,
its load() method
will be called, which should return a RouteCollection
containing Route objects.
Note
Routes loaded this way will be cached by the Router the same way as when they are defined in one of the default formats (e.g. XML, YML, PHP file).
Creating a custom Loader
To load routes from some custom source (i.e. from something other than annotations, YAML or XML files), you need to create a custom route loader. This loader has to implement LoaderInterface.
In most cases it is easier to extend from Loader instead of implementing LoaderInterface yourself.
The sample loader below supports loading routing resources with a type of
extra
. The type name should not clash with other loaders that might
support the same type of resource. Just make up a name specific to what
you do. The resource name itself is not actually used in the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
// src/AppBundle/Routing/ExtraLoader.php
namespace AppBundle\Routing;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class ExtraLoader extends Loader
{
private $loaded = false;
public function load($resource, $type = null)
{
if (true === $this->loaded) {
throw new \RuntimeException('Do not add the "extra" loader twice');
}
$routes = new RouteCollection();
// prepare a new route
$path = '/extra/{parameter}';
$defaults = array(
'_controller' => 'AppBundle:Extra:extra',
);
$requirements = array(
'parameter' => '\d+',
);
$route = new Route($path, $defaults, $requirements);
// add the new route to the route collection
$routeName = 'extraRoute';
$routes->add($routeName, $route);
$this->loaded = true;
return $routes;
}
public function supports($resource, $type = null)
{
return 'extra' === $type;
}
}
Make sure the controller you specify really exists. In this case you
have to create an extraAction
method in the ExtraController
of the AppBundle
:
1 2 3 4 5 6 7 8 9 10 11 12 13
// src/AppBundle/Controller/ExtraController.php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ExtraController extends Controller
{
public function extraAction($parameter)
{
return new Response($parameter);
}
}
Now define a service for the ExtraLoader
:
1 2 3 4 5 6
# app/config/services.yml
services:
app.routing_loader:
class: AppBundle\Routing\ExtraLoader
tags:
- { name: routing.loader }
1 2 3 4 5 6 7 8 9 10 11
<?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="app.routing_loader" class="AppBundle\Routing\ExtraLoader">
<tag name="routing.loader" />
</service>
</services>
</container>
1 2 3 4 5 6 7 8 9
use Symfony\Component\DependencyInjection\Definition;
$container
->setDefinition(
'app.routing_loader',
new Definition('AppBundle\Routing\ExtraLoader')
)
->addTag('routing.loader')
;
Notice the tag routing.loader
. All services with this tag will be marked
as potential route loaders and added as specialized route loaders to the
routing.loader
service, which is an instance of
DelegatingLoader.
Using the custom Loader
If you did nothing else, your custom routing loader would not be called. What remains to do is adding a few lines to the routing configuration:
1 2 3 4
# app/config/routing.yml
app_extra:
resource: .
type: extra
1 2 3 4 5 6 7
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<import resource="." type="extra" />
</routes>
1 2 3 4 5 6 7
// app/config/routing.php
use Symfony\Component\Routing\RouteCollection;
$collection = new RouteCollection();
$collection->addCollection($loader->import('.', 'extra'));
return $collection;
The important part here is the type
key. Its value should be "extra" as
this is the type which the ExtraLoader
supports and this will make sure
its load()
method gets called. The resource
key is insignificant
for the ExtraLoader
, so it is set to ".".
Note
The routes defined using custom route loaders will be automatically cached by the framework. So whenever you change something in the loader class itself, don't forget to clear the cache.
More advanced Loaders
If your custom route loader extends from Loader as shown above, you can also make use of the provided resolver, an instance of LoaderResolver, to load secondary routing resources.
Of course you still need to implement supports() and load(). Whenever you want to load another resource - for instance a YAML routing configuration file - you can call the import() method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// src/AppBundle/Routing/AdvancedLoader.php
namespace AppBundle\Routing;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Routing\RouteCollection;
class AdvancedLoader extends Loader
{
public function load($resource, $type = null)
{
$collection = new RouteCollection();
$resource = '@AppBundle/Resources/config/import_routing.yml';
$type = 'yaml';
$importedRoutes = $this->import($resource, $type);
$collection->addCollection($importedRoutes);
return $collection;
}
public function supports($resource, $type = null)
{
return 'advanced_extra' === $type;
}
}
Note
The resource name and type of the imported routing configuration can be anything that would normally be supported by the routing configuration loader (YAML, XML, PHP, annotation, etc.).