@@ -136,35 +136,80 @@ be prefixed with the string ``/site``.
136
136
Prefixing the Names of Imported Routes
137
137
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
138
138
139
- You also have the possibility to prefix all route names defined in a controller
140
- class with the `` name `` attribute of the `` @Route `` annotation: :
139
+ You also have the possibility to prefix the names of all the routes defined in
140
+ a controller class or imported from a configuration file :
141
141
142
- use Symfony\Component\Routing\Annotation\Route;
142
+ .. configuration-block ::
143
143
144
- /**
145
- * @Route(name="blog_")
146
- */
147
- class BlogController extends Controller
148
- {
149
- /**
150
- * @Route("/blog", name="index")
151
- */
152
- public function indexAction()
153
- {
154
- // ...
155
- }
144
+ .. code-block :: php-annotations
145
+
146
+ use Symfony\Component\Routing\Annotation\Route;
156
147
157
148
/**
158
- * @Route("/blog/posts/{slug}", name="post ")
149
+ * @Route(name="blog_ ")
159
150
*/
160
- public function showAction(Post $post)
151
+ class BlogController extends Controller
161
152
{
162
- // ...
153
+ /**
154
+ * @Route("/blog", name="index")
155
+ */
156
+ public function indexAction()
157
+ {
158
+ // ...
159
+ }
160
+
161
+ /**
162
+ * @Route("/blog/posts/{slug}", name="post")
163
+ */
164
+ public function showAction(Post $post)
165
+ {
166
+ // ...
167
+ }
163
168
}
164
- }
169
+
170
+ .. code-block :: yaml
171
+
172
+ # config/routes.yaml
173
+ controllers :
174
+ resource : ' ../src/Controller/'
175
+ type : annotation
176
+ name_prefix : ' blog_'
177
+
178
+ .. code-block :: xml
179
+
180
+ <!-- config/routes.xml -->
181
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
182
+ <routes xmlns =" http://symfony.com/schema/routing"
183
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
184
+ xsi : schemaLocation =" http://symfony.com/schema/routing
185
+ http://symfony.com/schema/routing/routing-1.0.xsd" >
186
+
187
+ <import
188
+ resource =" ../src/Controller/"
189
+ type =" annotation"
190
+ name-prefix =" blog_" />
191
+ </routes >
192
+
193
+ .. code-block :: php
194
+
195
+ // config/routes.php
196
+ use Symfony\Component\Routing\RouteCollection;
197
+
198
+ $app = $loader->import('../src/Controller/', 'annotation');
199
+ $app->addNamePrefix('blog_');
200
+
201
+ $collection = new RouteCollection();
202
+ $collection->addCollection($app);
203
+
204
+ return $collection;
165
205
166
206
In this example, the names of the routes will be ``blog_index `` and ``blog_post ``.
167
207
208
+ .. versionadded :: 4.1
209
+ The option to prefix route names in YAML, XML and PHP files was introduced
210
+ in Symfony 4.1. Previously only the ``@Route() `` annotation supported this
211
+ feature.
212
+
168
213
Adding a Host Requirement to Imported Routes
169
214
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
170
215
0 commit comments