@@ -7,7 +7,8 @@ How to Create a Custom Route Loader
7
7
Loading Routes
8
8
--------------
9
9
10
- The routes in a Symfony application are loaded by the :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Routing\\ DelegatingLoader `.
10
+ The routes in a Symfony application are loaded by the
11
+ :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Routing\\ DelegatingLoader `.
11
12
This loader uses several other loaders (delegates) to load resources of
12
13
different types, for instance Yaml files or ``@Route `` and ``@Method `` annotations
13
14
in controller files. The specialized loaders implement :class: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface `
@@ -25,7 +26,7 @@ Take these lines from ``routing.yml``:
25
26
26
27
The main loader tries all the delegate loaders and calls their
27
28
:method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::supports `
28
- with the given resource (" @AcmeDemoBundle/Controller/DemoController.php" )
29
+ with the given resource (`` @AcmeDemoBundle/Controller/DemoController.php `` )
29
30
and type ("annotation") as arguments. When one of the loader returns ``true ``,
30
31
its method :method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::load `
31
32
will be called, and the loader returns a :class: `Symfony\\ Component\\ Routing\\ RouteCollection `
@@ -34,14 +35,14 @@ containing :class:`Symfony\\Component\\Routing\\Route` objects.
34
35
Creating a Custom Loader
35
36
------------------------
36
37
37
- To load routes in another way then using annotations, Yaml or XML files,
38
+ To load routes in another way than using annotations, Yaml or XML files,
38
39
you need to create a custom route loader. This loader should implement
39
40
:class: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface `.
40
41
41
42
The sample loader below supports resources of type "extra". The resource
42
43
name itself is not used in the example::
43
44
44
- namespace Acme\DemoBundleBundle \Routing;
45
+ namespace Acme\DemoBundle \Routing;
45
46
46
47
use Symfony\Component\Config\Loader\LoaderInterface;
47
48
use Symfony\Component\Config\Loader\LoaderResolver;
@@ -118,7 +119,7 @@ Now define a service for the ``ExtraLoader``:
118
119
119
120
<services >
120
121
<service id =" acme_demo.routing_loader" class =" Acme\DemoBundle\Routing\ExtraLoader" >
121
- <tag name =" routing.loader" ></ tag >
122
+ <tag name =" routing.loader" / >
122
123
</service >
123
124
</services >
124
125
</container >
@@ -139,13 +140,34 @@ Notice the tag ``routing.loader``. All services with this tag will be marked
139
140
as potential route loaders and added as specialized routers to the
140
141
:class: `Symfony\\ Bundle\\ FrameworkBundle\\ Routing\\ DelegatingLoader `.
141
142
142
- Finally, we only need to add a few extra lines in `` routing.yml `` :
143
+ Finally, we only need to add a few extra lines to the routing configuration :
143
144
144
- .. code-block :: yaml
145
+ .. configuration-block ::
146
+
147
+ .. code-block :: yaml
148
+
149
+ AcmeDemoBundle_Extra :
150
+ resource : .
151
+ type : extra
152
+
153
+ .. code-block :: xml
154
+
155
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
156
+ <routes xmlns =" http://symfony.com/schema/routing"
157
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
158
+ xsi : schemaLocation =" http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd" >
159
+
160
+ <import resource =" ." type =" extra" />
161
+ </routes >
162
+
163
+ .. code-block :: php
164
+
165
+ use Symfony\Component\Routing\RouteCollection;
166
+
167
+ $collection = new RouteCollection();
168
+ $collection->addCollection($loader->import('.', 'extra'));
145
169
146
- AcmeDemoBundle_Extra :
147
- resource : .
148
- type : extra
170
+ return $collection;
149
171
150
172
The ``resource `` key is irrelevant, but required. The important part here
151
173
is the ``type `` key. Its value should be "extra". This is the type which
0 commit comments