@@ -14,13 +14,14 @@ slow down the installation process and make it error-prone.
14
14
Alternatively, you could also use a custom route loader when you want your
15
15
routes to be automatically generated or located based on some convention or
16
16
pattern. One example is the `FOSRestBundle `_ where routing is generated based
17
- off the names of the action methods in a controller.
17
+ on the names of the action methods in a controller.
18
18
19
19
.. note ::
20
20
21
21
There are many bundles out there that use their own route loaders to
22
22
accomplish cases like those described above, for instance
23
- `FOSRestBundle `_, `JMSI18nRoutingBundle `_, `KnpRadBundle `_ and `SonataAdminBundle `_.
23
+ `FOSRestBundle `_, `JMSI18nRoutingBundle `_, `KnpRadBundle `_ and
24
+ `SonataAdminBundle `_.
24
25
25
26
Loading Routes
26
27
--------------
@@ -35,20 +36,18 @@ and therefore have two important methods:
35
36
:method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::supports `
36
37
and :method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::load `.
37
38
38
- Take these lines from the ``routing.yml `` in the AcmeDemoBundle of the Standard
39
- Edition:
39
+ Take these lines from the ``routing.yml `` in the Symfony Standard Edition:
40
40
41
41
.. code-block :: yaml
42
42
43
- # src/Acme/DemoBundle/Resources /config/routing.yml
44
- _demo :
45
- resource : " @AcmeDemoBundle /Controller/DemoController.php "
43
+ # app /config/routing.yml
44
+ app :
45
+ resource : @AppBundle /Controller/
46
46
type : annotation
47
- prefix : /demo
48
47
49
- When the main loader parses this, it tries all the delegate loaders and calls
48
+ When the main loader parses this, it tries all registered delegate loaders and calls
50
49
their :method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::supports `
51
- method with the given resource (``@AcmeDemoBundle /Controller/DemoController.php ``)
50
+ method with the given resource (``@AppBundle /Controller/ ``)
52
51
and type (``annotation ``) as arguments. When one of the loader returns ``true ``,
53
52
its :method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::load ` method
54
53
will be called, which should return a :class: `Symfony\\ Component\\ Routing\\ RouteCollection `
@@ -59,13 +58,13 @@ Creating a custom Loader
59
58
60
59
To load routes from some custom source (i.e. from something other than annotations,
61
60
YAML or XML files), you need to create a custom route loader. This loader
62
- should implement :class: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface `.
61
+ has to implement :class: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface `.
63
62
64
63
The sample loader below supports loading routing resources with a type of
65
64
``extra ``. The type ``extra `` isn't important - you can just invent any resource
66
65
type you want. The resource name itself is not actually used in the example::
67
66
68
- namespace Acme\DemoBundle \Routing;
67
+ namespace AppBundle \Routing;
69
68
70
69
use Symfony\Component\Config\Loader\LoaderInterface;
71
70
use Symfony\Component\Config\Loader\LoaderResolverInterface;
@@ -87,14 +86,14 @@ type you want. The resource name itself is not actually used in the example::
87
86
// prepare a new route
88
87
$path = '/extra/{parameter}';
89
88
$defaults = array(
90
- '_controller' => 'AcmeDemoBundle:Demo :extra',
89
+ '_controller' => 'AppBundle:Extra :extra',
91
90
);
92
91
$requirements = array(
93
92
'parameter' => '\d+',
94
93
);
95
94
$route = new Route($path, $defaults, $requirements);
96
95
97
- // add the new route to the route collection:
96
+ // add the new route to the route collection
98
97
$routeName = 'extraRoute';
99
98
$routes->add($routeName, $route);
100
99
@@ -120,19 +119,32 @@ type you want. The resource name itself is not actually used in the example::
120
119
}
121
120
}
122
121
123
- .. note ::
122
+ Make sure the controller you specify really exists. In this case you
123
+ have to create an ``extraAction `` method in the ``ExtraController ``
124
+ of the ``AppBundle ``::
125
+
126
+ namespace AppBundle\Controller;
124
127
125
- Make sure the controller you specify really exists.
128
+ use Symfony\Component\HttpFoundation\Response;
129
+
130
+ class ExtraController extends Controller
131
+ {
132
+ public function extraAction($parameter)
133
+ {
134
+ return new Response($parameter);
135
+ }
136
+ }
126
137
127
138
Now define a service for the ``ExtraLoader ``:
128
139
129
140
.. configuration-block ::
130
141
131
142
.. code-block :: yaml
132
143
144
+ # app/config/services.yml
133
145
services :
134
- acme_demo .routing_loader :
135
- class : Acme\DemoBundle \Routing\ExtraLoader
146
+ app .routing_loader :
147
+ class : AppBundle \Routing\ExtraLoader
136
148
tags :
137
149
- { name: routing.loader }
138
150
@@ -144,7 +156,7 @@ Now define a service for the ``ExtraLoader``:
144
156
xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
145
157
146
158
<services >
147
- <service id =" acme_demo .routing_loader" class =" Acme\DemoBundle \Routing\ExtraLoader" >
159
+ <service id =" app .routing_loader" class =" AppBundle \Routing\ExtraLoader" >
148
160
<tag name =" routing.loader" />
149
161
</service >
150
162
</services >
@@ -156,14 +168,15 @@ Now define a service for the ``ExtraLoader``:
156
168
157
169
$container
158
170
->setDefinition(
159
- 'acme_demo .routing_loader',
160
- new Definition('Acme\DemoBundle \Routing\ExtraLoader')
171
+ 'app .routing_loader',
172
+ new Definition('AppBundle \Routing\ExtraLoader')
161
173
)
162
174
->addTag('routing.loader')
163
175
;
164
176
165
- Notice the tag ``routing.loader ``. All services with this tag will be marked
166
- as potential route loaders and added as specialized routers to the
177
+ Notice the tag ``routing.loader ``. All services with this *tag * will be marked
178
+ as potential route loaders and added as specialized route loaders to the
179
+ ``routing.loader `` *service *, which is an instance of
167
180
:class: `Symfony\\ Bundle\\ FrameworkBundle\\ Routing\\ DelegatingLoader `.
168
181
169
182
Using the custom Loader
@@ -177,7 +190,7 @@ Instead, you only need to add a few extra lines to the routing configuration:
177
190
.. code-block :: yaml
178
191
179
192
# app/config/routing.yml
180
- AcmeDemoBundle_Extra :
193
+ app_extra :
181
194
resource : .
182
195
type : extra
183
196
@@ -201,8 +214,8 @@ Instead, you only need to add a few extra lines to the routing configuration:
201
214
202
215
return $collection;
203
216
204
- The important part here is the ``type `` key. Its value should be "extra".
205
- This is the type which the ``ExtraLoader `` supports and this will make sure
217
+ The important part here is the ``type `` key. Its value should be "extra" as
218
+ this is the type which the ``ExtraLoader `` supports and this will make sure
206
219
its ``load() `` method gets called. The ``resource `` key is insignificant
207
220
for the ``ExtraLoader ``, so it is set to ".".
208
221
@@ -218,8 +231,9 @@ More advanced Loaders
218
231
In most cases it's better not to implement
219
232
:class: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface `
220
233
yourself, but extend from :class: `Symfony\\ Component\\ Config\\ Loader\\ Loader `.
221
- This class knows how to use a :class: `Symfony\\ Component\\ Config\\ Loader\\ LoaderResolver `
222
- to load secondary routing resources.
234
+ This class knows how to use a
235
+ :class: `Symfony\\ Component\\ Config\\ Loader\\ LoaderResolver ` to load secondary
236
+ routing resources.
223
237
224
238
Of course you still need to implement
225
239
:method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::supports `
@@ -228,7 +242,7 @@ Whenever you want to load another resource - for instance a YAML routing
228
242
configuration file - you can call the
229
243
:method: `Symfony\\ Component\\ Config\\ Loader\\ Loader::import ` method::
230
244
231
- namespace Acme\DemoBundle \Routing;
245
+ namespace AppBundle \Routing;
232
246
233
247
use Symfony\Component\Config\Loader\Loader;
234
248
use Symfony\Component\Routing\RouteCollection;
@@ -239,7 +253,7 @@ configuration file - you can call the
239
253
{
240
254
$collection = new RouteCollection();
241
255
242
- $resource = '@AcmeDemoBundle /Resources/config/import_routing.yml';
256
+ $resource = '@AppBundle /Resources/config/import_routing.yml';
243
257
$type = 'yaml';
244
258
245
259
$importedRoutes = $this->import($resource, $type);
@@ -251,7 +265,7 @@ configuration file - you can call the
251
265
252
266
public function supports($resource, $type = null)
253
267
{
254
- return $type === 'advanced_extra' ;
268
+ return 'advanced_extra' === $type ;
255
269
}
256
270
}
257
271
0 commit comments