1
1
.. index ::
2
2
single: Routing; Custom route loader
3
3
4
- How to Create a Custom Route Loader
4
+ How to create a custom Route Loader
5
5
===================================
6
6
7
- A custom route loader allows you to add routes to an application, without
8
- writing them down in for instance a Yaml file. This comes in handy when
9
- you have made a bundle and don't want to manually add the routes for the
10
- bundle to ``app/config/routing.yml ``. Especially when you want to make the
11
- bundle reusable, or when you have open-sourced it, this would slow down
12
- the installation process and make it error-prone.
7
+ A custom route loader allows you to add routes to an application without
8
+ including them, for example, in a Yaml file. This comes in handy when
9
+ you have a bundle but don't want to manually add the routes for the bundle
10
+ to ``app/config/routing.yml ``. This may be especially important when you want
11
+ to make the bundle reusable, or when you have open-sourced it as this would
12
+ slow down the installation process and make it error-prone.
13
13
14
- But you can also use a custom route loader when your routes correspond
15
- to a certain pattern and you don't want to or can't write them all out by
16
- hand. For instance when you have CRUD controllers of which the routes all
17
- correspond to a pattern like `` *_new ``, `` *_edit ``, etc .
14
+ Alternatively, you could also use a custom route loader when you want your
15
+ routes to be automatically generated or located based on some convention or
16
+ pattern. One example is the ` FOSRestBundle `_ where routing is generated based
17
+ off the names of the action methods in a controller .
18
18
19
19
.. note ::
20
20
@@ -44,23 +44,24 @@ Take these lines from ``routing.yml``:
44
44
type : annotation
45
45
prefix : /demo
46
46
47
- The main loader tries all the delegate loaders and calls their
48
- :method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::supports `
49
- with the given resource (``@AcmeDemoBundle/Controller/DemoController.php ``)
50
- and type (" annotation" ) as arguments. When one of the loader returns ``true ``,
51
- its method :method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::load `
52
- will be called, and the loader returns a :class: `Symfony\\ Component\\ Routing\\ RouteCollection `
47
+ When the main loader parses this, it tries all the delegate loaders and calls
48
+ their :method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::supports `
49
+ method with the given resource (``@AcmeDemoBundle/Controller/DemoController.php ``)
50
+ and type (`` annotation `` ) as arguments. When one of the loader returns ``true ``,
51
+ its :method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::load ` method
52
+ will be called, which should return a :class: `Symfony\\ Component\\ Routing\\ RouteCollection `
53
53
containing :class: `Symfony\\ Component\\ Routing\\ Route ` objects.
54
54
55
55
Creating a Custom Loader
56
56
------------------------
57
57
58
- To load routes in another way than using annotations, Yaml or XML files ,
59
- you need to create a custom route loader. This loader should implement
60
- :class: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface `.
58
+ To load routes from some custom source (i.e. from something other than annotations ,
59
+ Yaml or XML files), you need to create a custom route loader. This loader
60
+ should implement :class: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface `.
61
61
62
- The sample loader below supports resources of type "extra". The resource
63
- name itself is not used in the example::
62
+ The sample loader below supports loading routing resources with a type of
63
+ ``extra ``. The type ``extra `` isn't important - you can just invent any resource
64
+ type you want. The resource name itself is not actually used in the example::
64
65
65
66
namespace Acme\DemoBundle\Routing;
66
67
@@ -105,12 +106,13 @@ name itself is not used in the example::
105
106
106
107
public function getResolver()
107
108
{
108
- // will be explained later
109
+ // needed, but can be blank, unless you want to load other resources
110
+ // and if you do, using the Loader base class is easier (see below)
109
111
}
110
112
111
113
public function setResolver(LoaderResolver $resolver)
112
114
{
113
- // same here
115
+ // same as above
114
116
}
115
117
}
116
118
@@ -160,12 +162,17 @@ Notice the tag ``routing.loader``. All services with this tag will be marked
160
162
as potential route loaders and added as specialized routers to the
161
163
:class: `Symfony\\ Bundle\\ FrameworkBundle\\ Routing\\ DelegatingLoader `.
162
164
163
- Finally, we only need to add a few extra lines to the routing configuration:
165
+ Using the Custom Loader
166
+ ~~~~~~~~~~~~~~~~~~~~~~~
167
+
168
+ If you did nothing else, your custom routing loader would *not * be called.
169
+ Instead, you only need to add a few extra lines to the routing configuration:
164
170
165
171
.. configuration-block ::
166
172
167
173
.. code-block :: yaml
168
174
175
+ # app/config/routing.yml
169
176
AcmeDemoBundle_Extra :
170
177
resource : .
171
178
type : extra
@@ -182,6 +189,7 @@ Finally, we only need to add a few extra lines to the routing configuration:
182
189
183
190
.. code-block :: php
184
191
192
+ // app/config/routing.php
185
193
use Symfony\Component\Routing\RouteCollection;
186
194
187
195
$collection = new RouteCollection();
@@ -197,9 +205,8 @@ for the ``ExtraLoader``, so we set it to ".".
197
205
.. note ::
198
206
199
207
The routes defined using custom route loaders will be automatically
200
- cached by the framework. So whenever you change something to the behavior
201
- of the loader, don't forget to clear the cache.
202
-
208
+ cached by the framework. So whenever you change something in the loader
209
+ class itself, don't forget to clear the cache.
203
210
204
211
More Advanced Loaders
205
212
---------------------
@@ -213,8 +220,8 @@ to load secondary routing resources.
213
220
Of course you still need to implement
214
221
:method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::supports `
215
222
and :method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::load `.
216
- Whenever you want to load another resource, for instance a Yaml routing
217
- configuration file, you can call the
223
+ Whenever you want to load another resource - for instance a Yaml routing
224
+ configuration file - you can call the
218
225
:method: `Symfony\\ Component\\ Config\\ Loader\\ Loader::import ` method::
219
226
220
227
namespace Acme\DemoBundle\Routing;
0 commit comments