@@ -199,124 +199,6 @@ Use the ``methods`` option to restrict the verbs each route should respond to:
199
199
``_method `` with the method to use (e.g. ``<input type="hidden" name="_method" value="PUT" /> ``).
200
200
If you use :doc: `Symfony forms </forms >` this is done automatically for you.
201
201
202
- .. _routing-route-groups :
203
-
204
- Route Groups and Prefixes
205
- ~~~~~~~~~~~~~~~~~~~~~~~~~
206
-
207
- It's common for a group of routes to share some options (e.g. all routes related
208
- to the blog start with ``/blog ``) That's why Symfony includes a feature to share
209
- route configuration.
210
-
211
- When defining routes as annotations, put the common configuration in the
212
- ``@Route `` annotation of the controller class. In other routing formats, define
213
- the common configuration using options when importing the routes.
214
-
215
- .. configuration-block ::
216
-
217
- .. code-block :: php-annotations
218
-
219
- use Symfony\Component\Routing\Annotation\Route;
220
-
221
- /**
222
- * @Route("/blog", requirements={"locale": "en|es|fr"}, name="blog_")
223
- */
224
- class BlogController
225
- {
226
- /**
227
- * @Route("/{_locale}", name="index")
228
- */
229
- public function index()
230
- {
231
- // ...
232
- }
233
-
234
- /**
235
- * @Route("/{_locale}/posts/{slug}", name="post")
236
- */
237
- public function show(Post $post)
238
- {
239
- // ...
240
- }
241
- }
242
-
243
- .. code-block :: yaml
244
-
245
- # config/routes/annotations.yaml
246
- controllers :
247
- resource : ' ../src/Controller/'
248
- type : annotation
249
- # this is added to the beginning of all imported route URLs
250
- prefix : ' /blog'
251
- # this is added to the beginning of all imported route names
252
- name_prefix : ' blog_'
253
- # these requirements are added to all imported routes
254
- requirements :
255
- locale : ' en|es|fr'
256
- # An imported route with an empty URL will become "/blog/"
257
- # Uncomment this option to make that URL "/blog" instead
258
- # trailing_slash_on_root: false
259
-
260
- .. code-block :: xml
261
-
262
- <!-- config/routes/annotations.xml -->
263
- <?xml version =" 1.0" encoding =" UTF-8" ?>
264
- <routes xmlns =" http://symfony.com/schema/routing"
265
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
266
- xsi : schemaLocation =" http://symfony.com/schema/routing
267
- https://symfony.com/schema/routing/routing-1.0.xsd" >
268
-
269
- <!--
270
- the 'prefix' value is added to the beginning of all imported route URLs
271
- the 'name-prefix' value is added to the beginning of all imported route names
272
- -->
273
- <import resource =" ../src/Controller/"
274
- type =" annotation"
275
- prefix =" /blog"
276
- name-prefix =" blog_" >
277
- <!-- these requirements are added to all imported routes -->
278
- <requirement key =" locale" >en|es|fr</requirement >
279
- </import >
280
-
281
- <!-- An imported route with an empty URL will become "/blog/"
282
- Uncomment this option to make that URL "/blog" instead -->
283
- <import resource =" ../src/Controller/" type =" annotation"
284
- prefix =" /blog"
285
- trailing-slash-on-root =" false" >
286
- <!-- ... -->
287
- </import >
288
- </routes >
289
-
290
- .. code-block :: php
291
-
292
- // config/routes/annotations.php
293
- use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
294
-
295
- return function (RoutingConfigurator $routes) {
296
- $routes->import('../src/Controller/', 'annotation')
297
- // this is added to the beginning of all imported route URLs
298
- ->prefix('/blog')
299
- // An imported route with an empty URL will become "/blog/"
300
- // Pass FALSE as the second argument to make that URL "/blog" instead
301
- // ->prefix('/blog', false)
302
- // this is added to the beginning of all imported route names
303
- ->namePrefix('blog_')
304
- // these requirements are added to all imported routes
305
- ->requirements(['locale' => 'en|es|fr'])
306
- ;
307
- };
308
-
309
- In this example, the route of the ``index() `` action will be called ``blog_index ``
310
- and its URL will be ``/blog/ ``. The route of the ``show() `` action will be called
311
- ``blog_post `` and its URL will be ``/blog/{_locale}/posts/{slug} ``. Both routes
312
- will also validate that the ``_locale `` parameter matches the regular expression
313
- defined in the class annotation.
314
-
315
- .. seealso ::
316
-
317
- Symfony can :doc: `import routes from different sources </routing/custom_route_loader >`
318
- and you can even create your own route loader.
319
-
320
202
Debugging Routes
321
203
~~~~~~~~~~~~~~~~
322
204
@@ -1193,6 +1075,124 @@ A possible solution is to change the parameter requirements to be more permissiv
1193
1075
as the token and the format will be empty. This can be solved by replacing
1194
1076
the ``.+ `` requirement by ``[^.]+ `` to allow any character except dots.
1195
1077
1078
+ .. _routing-route-groups :
1079
+
1080
+ Route Groups and Prefixes
1081
+ -------------------------
1082
+
1083
+ It's common for a group of routes to share some options (e.g. all routes related
1084
+ to the blog start with ``/blog ``) That's why Symfony includes a feature to share
1085
+ route configuration.
1086
+
1087
+ When defining routes as annotations, put the common configuration in the
1088
+ ``@Route `` annotation of the controller class. In other routing formats, define
1089
+ the common configuration using options when importing the routes.
1090
+
1091
+ .. configuration-block ::
1092
+
1093
+ .. code-block :: php-annotations
1094
+
1095
+ use Symfony\Component\Routing\Annotation\Route;
1096
+
1097
+ /**
1098
+ * @Route("/blog", requirements={"locale": "en|es|fr"}, name="blog_")
1099
+ */
1100
+ class BlogController
1101
+ {
1102
+ /**
1103
+ * @Route("/{_locale}", name="index")
1104
+ */
1105
+ public function index()
1106
+ {
1107
+ // ...
1108
+ }
1109
+
1110
+ /**
1111
+ * @Route("/{_locale}/posts/{slug}", name="post")
1112
+ */
1113
+ public function show(Post $post)
1114
+ {
1115
+ // ...
1116
+ }
1117
+ }
1118
+
1119
+ .. code-block :: yaml
1120
+
1121
+ # config/routes/annotations.yaml
1122
+ controllers :
1123
+ resource : ' ../src/Controller/'
1124
+ type : annotation
1125
+ # this is added to the beginning of all imported route URLs
1126
+ prefix : ' /blog'
1127
+ # this is added to the beginning of all imported route names
1128
+ name_prefix : ' blog_'
1129
+ # these requirements are added to all imported routes
1130
+ requirements :
1131
+ locale : ' en|es|fr'
1132
+ # An imported route with an empty URL will become "/blog/"
1133
+ # Uncomment this option to make that URL "/blog" instead
1134
+ # trailing_slash_on_root: false
1135
+
1136
+ .. code-block :: xml
1137
+
1138
+ <!-- config/routes/annotations.xml -->
1139
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1140
+ <routes xmlns =" http://symfony.com/schema/routing"
1141
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1142
+ xsi : schemaLocation =" http://symfony.com/schema/routing
1143
+ https://symfony.com/schema/routing/routing-1.0.xsd" >
1144
+
1145
+ <!--
1146
+ the 'prefix' value is added to the beginning of all imported route URLs
1147
+ the 'name-prefix' value is added to the beginning of all imported route names
1148
+ -->
1149
+ <import resource =" ../src/Controller/"
1150
+ type =" annotation"
1151
+ prefix =" /blog"
1152
+ name-prefix =" blog_" >
1153
+ <!-- these requirements are added to all imported routes -->
1154
+ <requirement key =" locale" >en|es|fr</requirement >
1155
+ </import >
1156
+
1157
+ <!-- An imported route with an empty URL will become "/blog/"
1158
+ Uncomment this option to make that URL "/blog" instead -->
1159
+ <import resource =" ../src/Controller/" type =" annotation"
1160
+ prefix =" /blog"
1161
+ trailing-slash-on-root =" false" >
1162
+ <!-- ... -->
1163
+ </import >
1164
+ </routes >
1165
+
1166
+ .. code-block :: php
1167
+
1168
+ // config/routes/annotations.php
1169
+ use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
1170
+
1171
+ return function (RoutingConfigurator $routes) {
1172
+ $routes->import('../src/Controller/', 'annotation')
1173
+ // this is added to the beginning of all imported route URLs
1174
+ ->prefix('/blog')
1175
+ // An imported route with an empty URL will become "/blog/"
1176
+ // Pass FALSE as the second argument to make that URL "/blog" instead
1177
+ // ->prefix('/blog', false)
1178
+ // this is added to the beginning of all imported route names
1179
+ ->namePrefix('blog_')
1180
+ // these requirements are added to all imported routes
1181
+ ->requirements(['locale' => 'en|es|fr'])
1182
+ ;
1183
+ };
1184
+
1185
+ In this example, the route of the ``index() `` action will be called ``blog_index ``
1186
+ and its URL will be ``/blog/ ``. The route of the ``show() `` action will be called
1187
+ ``blog_post `` and its URL will be ``/blog/{_locale}/posts/{slug} ``. Both routes
1188
+ will also validate that the ``_locale `` parameter matches the regular expression
1189
+ defined in the class annotation.
1190
+
1191
+ .. seealso ::
1192
+
1193
+ Symfony can :doc: `import routes from different sources </routing/custom_route_loader >`
1194
+ and you can even create your own route loader.
1195
+
1196
1196
Getting the Route Name and Parameters
1197
1197
-------------------------------------
1198
1198
0 commit comments