4
4
How to Include External Routing Resources
5
5
=========================================
6
6
7
- All routes are loaded via a single configuration file - usually ``app/config/routing.yml ``
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 ``app/config/routing.yml `` (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:
11
14
12
15
.. configuration-block ::
13
16
14
17
.. code-block :: yaml
15
18
16
19
# app/config/routing.yml
17
- app :
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
18
26
resource : ' @AppBundle/Controller/'
19
- type : annotation # required to enable the Annotation reader for this resource
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
20
38
21
39
.. code-block :: xml
22
40
@@ -27,8 +45,17 @@ This can be done by "importing" directories into the routing configuration:
27
45
xsi : schemaLocation =" http://symfony.com/schema/routing
28
46
http://symfony.com/schema/routing/routing-1.0.xsd" >
29
47
30
- <!-- the type is required to enable the annotation reader for this resource -->
31
- <import resource =" @AppBundle/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 =" @AppBundle/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" />
32
59
</routes >
33
60
34
61
.. code-block :: php
@@ -38,60 +65,26 @@ This can be done by "importing" directories into the routing configuration:
38
65
39
66
$collection = new RouteCollection();
40
67
$collection->addCollection(
41
- // second argument is the type, which is required to enable
42
- // the annotation reader for this resource
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
43
72
$loader->import("@AppBundle/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")
44
79
);
45
80
46
81
return $collection;
47
82
48
83
.. note ::
49
84
50
- When importing resources from YAML, the key (e.g. ``app ``) is meaningless.
85
+ When importing resources from YAML, the key (e.g. ``app_file ``) is meaningless.
51
86
Just be sure that it's unique so no other lines override it.
52
87
53
- The ``resource `` key loads the given routing resource. In this example the
54
- resource is a directory, where the ``@AppBundle `` shortcut syntax resolves
55
- to the full path of the AppBundle. When pointing to a directory, all files
56
- in that directory are parsed and put into the routing.
57
-
58
- .. note ::
59
-
60
- You can also include other routing configuration files, this is often
61
- used to import the routing of third party bundles:
62
-
63
- .. configuration-block ::
64
-
65
- .. code-block :: yaml
66
-
67
- # app/config/routing.yml
68
- app :
69
- resource : ' @AcmeOtherBundle/Resources/config/routing.yml'
70
-
71
- .. code-block :: xml
72
-
73
- <!-- app/config/routing.xml -->
74
- <?xml version =" 1.0" encoding =" UTF-8" ?>
75
- <routes xmlns =" http://symfony.com/schema/routing"
76
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
77
- xsi : schemaLocation =" http://symfony.com/schema/routing
78
- http://symfony.com/schema/routing/routing-1.0.xsd" >
79
-
80
- <import resource =" @AcmeOtherBundle/Resources/config/routing.xml" />
81
- </routes >
82
-
83
- .. code-block :: php
84
-
85
- // app/config/routing.php
86
- use Symfony\Component\Routing\RouteCollection;
87
-
88
- $collection = new RouteCollection();
89
- $collection->addCollection(
90
- $loader->import("@AcmeOtherBundle/Resources/config/routing.php")
91
- );
92
-
93
- return $collection;
94
-
95
88
Prefixing Imported Routes
96
89
~~~~~~~~~~~~~~~~~~~~~~~~~
97
90
0 commit comments