@@ -14,13 +14,14 @@ slow down the installation process and make it error-prone.
1414Alternatively, you could also use a custom route loader when you want your
1515routes to be automatically generated or located based on some convention or
1616pattern. 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.
1818
1919.. note ::
2020
2121 There are many bundles out there that use their own route loaders to
2222 accomplish cases like those described above, for instance
23- `FOSRestBundle `_, `JMSI18nRoutingBundle `_, `KnpRadBundle `_ and `SonataAdminBundle `_.
23+ `FOSRestBundle `_, `JMSI18nRoutingBundle `_, `KnpRadBundle `_ and
24+ `SonataAdminBundle `_.
2425
2526Loading Routes
2627--------------
@@ -35,20 +36,18 @@ and therefore have two important methods:
3536:method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::supports `
3637and :method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::load `.
3738
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:
4040
4141.. code-block :: yaml
4242
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/
4646 type : annotation
47- prefix : /demo
4847
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
5049their :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/ ``)
5251and type (``annotation ``) as arguments. When one of the loader returns ``true ``,
5352its :method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::load ` method
5453will be called, which should return a :class: `Symfony\\ Component\\ Routing\\ RouteCollection `
@@ -59,13 +58,13 @@ Creating a custom Loader
5958
6059To load routes from some custom source (i.e. from something other than annotations,
6160YAML 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 `.
6362
6463The sample loader below supports loading routing resources with a type of
6564``extra ``. The type ``extra `` isn't important - you can just invent any resource
6665type you want. The resource name itself is not actually used in the example::
6766
68- namespace Acme\DemoBundle \Routing;
67+ namespace AppBundle \Routing;
6968
7069 use Symfony\Component\Config\Loader\LoaderInterface;
7170 use Symfony\Component\Config\Loader\LoaderResolverInterface;
@@ -87,14 +86,14 @@ type you want. The resource name itself is not actually used in the example::
8786 // prepare a new route
8887 $path = '/extra/{parameter}';
8988 $defaults = array(
90- '_controller' => 'AcmeDemoBundle:Demo :extra',
89+ '_controller' => 'AppBundle:Extra :extra',
9190 );
9291 $requirements = array(
9392 'parameter' => '\d+',
9493 );
9594 $route = new Route($path, $defaults, $requirements);
9695
97- // add the new route to the route collection:
96+ // add the new route to the route collection
9897 $routeName = 'extraRoute';
9998 $routes->add($routeName, $route);
10099
@@ -120,19 +119,32 @@ type you want. The resource name itself is not actually used in the example::
120119 }
121120 }
122121
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;
124127
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+ }
126137
127138Now define a service for the ``ExtraLoader ``:
128139
129140.. configuration-block ::
130141
131142 .. code-block :: yaml
132143
144+ # app/config/services.yml
133145 services :
134- acme_demo .routing_loader :
135- class : Acme\DemoBundle \Routing\ExtraLoader
146+ app .routing_loader :
147+ class : AppBundle \Routing\ExtraLoader
136148 tags :
137149 - { name: routing.loader }
138150
@@ -144,7 +156,7 @@ Now define a service for the ``ExtraLoader``:
144156 xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
145157
146158 <services >
147- <service id =" acme_demo .routing_loader" class =" Acme\DemoBundle \Routing\ExtraLoader" >
159+ <service id =" app .routing_loader" class =" AppBundle \Routing\ExtraLoader" >
148160 <tag name =" routing.loader" />
149161 </service >
150162 </services >
@@ -156,14 +168,15 @@ Now define a service for the ``ExtraLoader``:
156168
157169 $container
158170 ->setDefinition(
159- 'acme_demo .routing_loader',
160- new Definition('Acme\DemoBundle \Routing\ExtraLoader')
171+ 'app .routing_loader',
172+ new Definition('AppBundle \Routing\ExtraLoader')
161173 )
162174 ->addTag('routing.loader')
163175 ;
164176
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
167180:class: `Symfony\\ Bundle\\ FrameworkBundle\\ Routing\\ DelegatingLoader `.
168181
169182Using the custom Loader
@@ -177,7 +190,7 @@ Instead, you only need to add a few extra lines to the routing configuration:
177190 .. code-block :: yaml
178191
179192 # app/config/routing.yml
180- AcmeDemoBundle_Extra :
193+ app_extra :
181194 resource : .
182195 type : extra
183196
@@ -201,8 +214,8 @@ Instead, you only need to add a few extra lines to the routing configuration:
201214
202215 return $collection;
203216
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
206219its ``load() `` method gets called. The ``resource `` key is insignificant
207220for the ``ExtraLoader ``, so it is set to ".".
208221
@@ -218,8 +231,9 @@ More advanced Loaders
218231In most cases it's better not to implement
219232:class: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface `
220233yourself, 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.
223237
224238Of course you still need to implement
225239:method: `Symfony\\ Component\\ Config\\ Loader\\ LoaderInterface::supports `
@@ -228,7 +242,7 @@ Whenever you want to load another resource - for instance a YAML routing
228242configuration file - you can call the
229243:method: `Symfony\\ Component\\ Config\\ Loader\\ Loader::import ` method::
230244
231- namespace Acme\DemoBundle \Routing;
245+ namespace AppBundle \Routing;
232246
233247 use Symfony\Component\Config\Loader\Loader;
234248 use Symfony\Component\Routing\RouteCollection;
@@ -239,7 +253,7 @@ configuration file - you can call the
239253 {
240254 $collection = new RouteCollection();
241255
242- $resource = '@AcmeDemoBundle /Resources/config/import_routing.yml';
256+ $resource = '@AppBundle /Resources/config/import_routing.yml';
243257 $type = 'yaml';
244258
245259 $importedRoutes = $this->import($resource, $type);
@@ -251,7 +265,7 @@ configuration file - you can call the
251265
252266 public function supports($resource, $type = null)
253267 {
254- return $type === 'advanced_extra' ;
268+ return 'advanced_extra' === $type ;
255269 }
256270 }
257271
0 commit comments