44How to Include External Routing Resources
55=========================================
66
7- All routes are loaded via a single configuration file - usually ``config/routes.yaml ``
8- (see :ref: `routing-creating-routes `). However, if you use routing annotations,
9- you'll need to point the router to the controllers with the annotations.
10- This can be done by "importing" directories into the routing configuration:
7+ Simple applications can define all their routes in a single configuration file -
8+ usually ``config/routes.yaml `` (see :ref: `routing-creating-routes `).
9+ However, in most applications it's common to import routes definitions from
10+ different resources: PHP annotations in controller files, YAML or XML files
11+ stored in some directory, etc.
12+
13+ This can be done by importing routing resources from the main routing file:
1114
1215.. configuration-block ::
1316
1417 .. code-block :: yaml
1518
1619 # config/routes.yaml
17- controllers :
18- resource : ../src/Controller/
19- type : annotation
20+ app_file :
21+ # loads routes from the given routing file stored in some bundle
22+ resource : ' @AcmeOtherBundle/Resources/config/routing.yml'
23+
24+ app_annotations :
25+ # loads routes from the PHP annotations of the controllers found in that directory
26+ resource : ' ../src/Controller/'
27+ type : annotation
28+
29+ app_directory :
30+ # loads routes from the YAML or XML files found in that directory
31+ resource : ' ../legacy/routing/'
32+ type : directory
33+
34+ app_bundle :
35+ # loads routes from the YAML or XML files found in some bundle directory
36+ resource : ' @AppBundle/Resources/config/routing/public/'
37+ type : directory
2038
2139 .. code-block :: xml
2240
@@ -27,7 +45,17 @@ This can be done by "importing" directories into the routing configuration:
2745 xsi : schemaLocation =" http://symfony.com/schema/routing
2846 http://symfony.com/schema/routing/routing-1.0.xsd" >
2947
30- <import resource =" ../src/Controller/" type =" annotation" />
48+ <!-- loads routes from the given routing file stored in some bundle -->
49+ <import resource =" @AcmeOtherBundle/Resources/config/routing.yml" />
50+
51+ <!-- loads routes from the PHP annotations of the controllers found in that directory -->
52+ <import resource =" ../src/Controller/" type =" annotation" />
53+
54+ <!-- loads routes from the YAML or XML files found in that directory -->
55+ <import resource =" ../legacy/routing/" type =" directory" />
56+
57+ <!-- loads routes from the YAML or XML files found in some bundle directory -->
58+ <import resource =" @AppBundle/Resources/config/routing/public/" type =" directory" />
3159 </routes >
3260
3361 .. code-block :: php
@@ -37,56 +65,26 @@ This can be done by "importing" directories into the routing configuration:
3765
3866 $collection = new RouteCollection();
3967 $collection->addCollection(
68+ // loads routes from the given routing file stored in some bundle
69+ $loader->import("@AcmeOtherBundle/Resources/config/routing.yml")
70+
71+ // loads routes from the PHP annotations of the controllers found in that directory
4072 $loader->import("../src/Controller/", "annotation")
73+
74+ // loads routes from the YAML or XML files found in that directory
75+ $loader->import("../legacy/routing/", "directory")
76+
77+ // loads routes from the YAML or XML files found in some bundle directory
78+ $loader->import("@AppBundle/Resources/config/routing/public/", "directory")
4179 );
4280
4381 return $collection;
4482
4583 .. note ::
4684
47- When importing resources from YAML, the key (e.g. ``controllers ``) is meaningless.
85+ When importing resources from YAML, the key (e.g. ``app_file ``) is meaningless.
4886 Just be sure that it's unique so no other lines override it.
4987
50- The ``resource `` key loads the given routing resource. In this example the
51- resource is a directory and all files in that directory are parsed and put into
52- the routing.
53-
54- .. note ::
55-
56- You can also include other routing configuration files:
57-
58- .. configuration-block ::
59-
60- .. code-block :: yaml
61-
62- # config/routes.yaml
63- app :
64- resource : ' @ThirdPartyBundle/Resources/config/routing.yaml'
65-
66- .. code-block :: xml
67-
68- <!-- config/routes.xml -->
69- <?xml version =" 1.0" encoding =" UTF-8" ?>
70- <routes xmlns =" http://symfony.com/schema/routing"
71- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
72- xsi : schemaLocation =" http://symfony.com/schema/routing
73- http://symfony.com/schema/routing/routing-1.0.xsd" >
74-
75- <import resource =" @ThirdPartyBundle/Resources/config/routing.xml" />
76- </routes >
77-
78- .. code-block :: php
79-
80- // config/routes.php
81- use Symfony\Component\Routing\RouteCollection;
82-
83- $collection = new RouteCollection();
84- $collection->addCollection(
85- $loader->import("@ThirdPartyBundle/Resources/config/routing.php")
86- );
87-
88- return $collection;
89-
9088Prefixing Imported Routes
9189~~~~~~~~~~~~~~~~~~~~~~~~~
9290
0 commit comments