@@ -1150,17 +1150,19 @@ Including External Routing Resources
1150
1150
------------------------------------
1151
1151
1152
1152
All 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:
1156
1157
1157
1158
.. configuration-block ::
1158
1159
1159
1160
.. code-block :: yaml
1160
1161
1161
1162
# app/config/routing.yml
1162
1163
app :
1163
- resource : " @AppBundle/Resources/config/routing.yml"
1164
+ resource : " @AppBundle/Controller/"
1165
+ type : annotation # required to enable the Annotation reader for this resource
1164
1166
1165
1167
.. code-block :: xml
1166
1168
@@ -1171,7 +1173,8 @@ inside a bundle. This can be done by "importing" that file:
1171
1173
xsi : schemaLocation =" http://symfony.com/schema/routing
1172
1174
http://symfony.com/schema/routing/routing-1.0.xsd" >
1173
1175
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" />
1175
1178
</routes >
1176
1179
1177
1180
.. code-block :: php
@@ -1181,7 +1184,9 @@ inside a bundle. This can be done by "importing" that file:
1181
1184
1182
1185
$collection = new RouteCollection();
1183
1186
$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")
1185
1190
);
1186
1191
1187
1192
return $collection;
@@ -1192,64 +1197,63 @@ inside a bundle. This can be done by "importing" that file:
1192
1197
Just be sure that it's unique so no other lines override it.
1193
1198
1194
1199
The ``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.
1198
1203
1199
- .. configuration-block ::
1204
+ .. note ::
1200
1205
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:
1202
1208
1203
- # src/AppBundle/Resources/config/routing.yml
1204
- app :
1205
- path : /hello/{name}
1206
- defaults : { _controller: AppBundle:Hello:index }
1209
+ .. configuration-block ::
1207
1210
1208
- .. code-block :: xml
1211
+ .. code-block :: yaml
1209
1212
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"
1216
1216
1217
- <route id =" acme_hello" path =" /hello/{name}" >
1218
- <default key =" _controller" >AppBundle:Hello:index</default >
1219
- </route >
1220
- </routes >
1217
+ .. code-block :: xml
1221
1218
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" >
1223
1225
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 >
1227
1228
1228
- $collection = new RouteCollection();
1229
- $collection->add('acme_hello', new Route('/hello/{name}', array(
1230
- '_controller' => 'AppBundle:Hello:index',
1231
- )));
1229
+ .. code-block :: php
1232
1230
1233
- return $collection;
1231
+ // app/config/routing.php
1232
+ use Symfony\Component\Routing\RouteCollection;
1234
1233
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;
1237
1240
1238
1241
Prefixing Imported Routes
1239
1242
~~~~~~~~~~~~~~~~~~~~~~~~~
1240
1243
1241
1244
You 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 } ``) :
1244
1247
1245
1248
.. configuration-block ::
1246
1249
1247
1250
.. code-block :: yaml
1248
1251
1249
1252
# app/config/routing.yml
1250
1253
app :
1251
- resource : " @AppBundle/Resources/config/routing.yml"
1252
- prefix : /admin
1254
+ resource : " @AppBundle/Controller/"
1255
+ type : annotation
1256
+ prefix : /site
1253
1257
1254
1258
.. code-block :: xml
1255
1259
@@ -1261,32 +1265,27 @@ instead of simply ``/hello/{name}``:
1261
1265
http://symfony.com/schema/routing/routing-1.0.xsd" >
1262
1266
1263
1267
<import
1264
- resource =" @AppBundle/Resources/config/routing.xml"
1265
- prefix =" /admin" />
1268
+ resource =" @AppBundle/Controller/"
1269
+ type =" annotation"
1270
+ prefix =" /site" />
1266
1271
</routes >
1267
1272
1268
1273
.. code-block :: php
1269
1274
1270
1275
// app/config/routing.php
1271
1276
use Symfony\Component\Routing\RouteCollection;
1272
1277
1273
- $app = $loader->import('@AppBundle/Resources/config/routing.php ');
1274
- $app->addPrefix('/admin ');
1278
+ $app = $loader->import('@AppBundle/Controller/ ');
1279
+ $app->addPrefix('/site ');
1275
1280
1276
1281
$collection = new RouteCollection();
1277
1282
$collection->addCollection($app);
1278
1283
1279
1284
return $collection;
1280
1285
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
1282
1287
from the new routing resource.
1283
1288
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
-
1290
1289
Adding a Host Requirement to Imported Routes
1291
1290
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1292
1291
0 commit comments