@@ -1150,17 +1150,19 @@ Including External Routing Resources
11501150------------------------------------
11511151
11521152All routes are loaded via a single configuration file - usually
1153- ``app/config/routing.yml `` (see `Creating Routes `_ above). Commonly, however,
1154- you'll want to load routes from other places, like a routing file that lives
1155- inside a bundle. This can be done by "importing" that file:
1153+ ``app/config/routing.yml `` (see `Creating Routes `_ above). However, if you use
1154+ routing annotations, you'll need to point the router to the controllers with
1155+ the annotations. This can be done by "importing" directories into the routing
1156+ configuration:
11561157
11571158.. configuration-block ::
11581159
11591160 .. code-block :: yaml
11601161
11611162 # app/config/routing.yml
11621163 app :
1163- resource : " @AppBundle/Resources/config/routing.yml"
1164+ resource : " @AppBundle/Controller/"
1165+ type : annotation # required to enable the Annotation reader for this resource
11641166
11651167 .. code-block :: xml
11661168
@@ -1171,7 +1173,8 @@ inside a bundle. This can be done by "importing" that file:
11711173 xsi : schemaLocation =" http://symfony.com/schema/routing
11721174 http://symfony.com/schema/routing/routing-1.0.xsd" >
11731175
1174- <import resource =" @AppBundle/Resources/config/routing.xml" />
1176+ <!-- the type is required to enable the annotation reader for this resource -->
1177+ <import resource =" @AppBundle/Controller/" type =" annotation" />
11751178 </routes >
11761179
11771180 .. code-block :: php
@@ -1181,7 +1184,9 @@ inside a bundle. This can be done by "importing" that file:
11811184
11821185 $collection = new RouteCollection();
11831186 $collection->addCollection(
1184- $loader->import("@AppBundle/Resources/config/routing.php")
1187+ // second argument is the type, which is required to enable the annotation reader
1188+ // for this resource
1189+ $loader->import("@AppBundle/Controller/", "annotation")
11851190 );
11861191
11871192 return $collection;
@@ -1192,64 +1197,63 @@ inside a bundle. This can be done by "importing" that file:
11921197 Just be sure that it's unique so no other lines override it.
11931198
11941199The ``resource `` key loads the given routing resource. In this example the
1195- resource is the full path to a file , where the ``@AppBundle `` shortcut
1196- syntax resolves to the path of that bundle. The imported file might look
1197- like this:
1200+ resource is a directory , where the ``@AppBundle `` shortcut syntax resolves to
1201+ the full path of the AppBundle. When pointing to a directory, all files in that
1202+ directory are parsed and put into the routing.
11981203
1199- .. configuration-block ::
1204+ .. note ::
12001205
1201- .. code-block :: yaml
1206+ You can also include other routing configuration files, this is often used
1207+ to import the routing of third party bundles:
12021208
1203- # src/AppBundle/Resources/config/routing.yml
1204- app :
1205- path : /hello/{name}
1206- defaults : { _controller: AppBundle:Hello:index }
1209+ .. configuration-block ::
12071210
1208- .. code-block :: xml
1211+ .. code-block :: yaml
12091212
1210- <!-- src/AppBundle/Resources/config/routing.xml -->
1211- <?xml version =" 1.0" encoding =" UTF-8" ?>
1212- <routes xmlns =" http://symfony.com/schema/routing"
1213- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1214- xsi : schemaLocation =" http://symfony.com/schema/routing
1215- http://symfony.com/schema/routing/routing-1.0.xsd" >
1213+ # app/config/routing.yml
1214+ app :
1215+ resource : " @AcmeOtherBundle/Resources/config/routing.yml"
12161216
1217- <route id =" acme_hello" path =" /hello/{name}" >
1218- <default key =" _controller" >AppBundle:Hello:index</default >
1219- </route >
1220- </routes >
1217+ .. code-block :: xml
12211218
1222- .. code-block :: php
1219+ <!-- app/config/routing.xml -->
1220+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1221+ <routes xmlns =" http://symfony.com/schema/routing"
1222+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1223+ xsi : schemaLocation =" http://symfony.com/schema/routing
1224+ http://symfony.com/schema/routing/routing-1.0.xsd" >
12231225
1224- // src/AppBundle/Resources/config/routing.php
1225- use Symfony\Component\Routing\RouteCollection;
1226- use Symfony\Component\Routing\Route;
1226+ <import resource =" @AcmeOtherBundle/Resources/config/routing.xml" />
1227+ </routes >
12271228
1228- $collection = new RouteCollection();
1229- $collection->add('acme_hello', new Route('/hello/{name}', array(
1230- '_controller' => 'AppBundle:Hello:index',
1231- )));
1229+ .. code-block :: php
12321230
1233- return $collection;
1231+ // app/config/routing.php
1232+ use Symfony\Component\Routing\RouteCollection;
12341233
1235- The routes from this file are parsed and loaded in the same way as the main
1236- routing file.
1234+ $collection = new RouteCollection();
1235+ $collection->addCollection(
1236+ $loader->import("@AcmeOtherBundle/Resources/config/routing.php")
1237+ );
1238+
1239+ return $collection;
12371240
12381241 Prefixing Imported Routes
12391242~~~~~~~~~~~~~~~~~~~~~~~~~
12401243
12411244You can also choose to provide a "prefix" for the imported routes. For example,
1242- suppose you want the `` app `` routes to have a final path of ``/admin/hello/{name} ``
1243- instead of simply ``/hello/{name } ``:
1245+ suppose you want to prefix all routes in the AppBundle with ``/site `` (e.g.
1246+ `` /site/blog/{slug} `` instead of ``/blog/{slug } ``) :
12441247
12451248.. configuration-block ::
12461249
12471250 .. code-block :: yaml
12481251
12491252 # app/config/routing.yml
12501253 app :
1251- resource : " @AppBundle/Resources/config/routing.yml"
1252- prefix : /admin
1254+ resource : " @AppBundle/Controller/"
1255+ type : annotation
1256+ prefix : /site
12531257
12541258 .. code-block :: xml
12551259
@@ -1261,32 +1265,27 @@ instead of simply ``/hello/{name}``:
12611265 http://symfony.com/schema/routing/routing-1.0.xsd" >
12621266
12631267 <import
1264- resource =" @AppBundle/Resources/config/routing.xml"
1265- prefix =" /admin" />
1268+ resource =" @AppBundle/Controller/"
1269+ type =" annotation"
1270+ prefix =" /site" />
12661271 </routes >
12671272
12681273 .. code-block :: php
12691274
12701275 // app/config/routing.php
12711276 use Symfony\Component\Routing\RouteCollection;
12721277
1273- $app = $loader->import('@AppBundle/Resources/config/routing.php ');
1274- $app->addPrefix('/admin ');
1278+ $app = $loader->import('@AppBundle/Controller/ ');
1279+ $app->addPrefix('/site ');
12751280
12761281 $collection = new RouteCollection();
12771282 $collection->addCollection($app);
12781283
12791284 return $collection;
12801285
1281- The string ``/admin `` will now be prepended to the path of each route loaded
1286+ The string ``/site `` will now be prepended to the path of each route loaded
12821287from the new routing resource.
12831288
1284- .. tip ::
1285-
1286- You can also define routes using annotations. See the
1287- :doc: `FrameworkExtraBundle documentation </bundles/SensioFrameworkExtraBundle/annotations/routing >`
1288- to see how.
1289-
12901289Adding a Host Requirement to Imported Routes
12911290~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12921291
0 commit comments