4
4
Routing
5
5
=======
6
6
7
- When your application receives an HTTP request, it executes a
8
- :doc: `controller action </controller >` to generate the HTTP response. The
9
- routing configuration defines which action to run for each incoming URL. It also
10
- provides other useful features, like generating SEO-friendly URLs
11
- (e.g. ``/read/intro-to-symfony `` instead of ``index.php?article_id=57 ``).
7
+ When your application receives a request, it executes a
8
+ :doc: `controller action </controller >` to generate the response. The routing
9
+ configuration defines which action to run for each incoming URL. It also
10
+ provides other useful features, like generating SEO-friendly URLs (e.g.
11
+ ``/read/intro-to-symfony `` instead of ``index.php?article_id=57 ``).
12
12
13
13
Creating Routes
14
14
---------------
@@ -332,47 +332,41 @@ with a locale. This can be done by defining a different prefix for each locale
332
332
Route Groups
333
333
~~~~~~~~~~~~
334
334
335
- It's common for a group of routes to share some options (e.g. all routes
336
- related to the blog start with ``/blog ``; all routes validate the locale with
337
- the same regular expression; etc.) That's why Symfony includes a feature to
338
- share route configuration.
335
+ It's common for a group of routes to share some options (e.g. all routes related
336
+ to the blog start with ``/blog ``) That's why Symfony includes a feature to share
337
+ route configuration.
339
338
340
- When defining routes as annotations, add an ``@Route `` annotation to the
341
- controller class to apply options to all the routes defined in that class::
339
+ When defining routes as annotations, put the common configuration in the
340
+ ``@Route `` annotation of the controller class. In other routing formats, define
341
+ the common configuration using options when importing the routes.
342
342
343
- use Symfony\Component\Routing\Annotation\Route;
343
+ .. configuration-block ::
344
344
345
- /**
346
- * @Route("/blog", requirements={"locale": "en|es|fr"}, name="blog_")
347
- */
348
- class BlogController
349
- {
350
- /**
351
- * @Route("/{_locale}", name="index")
352
- */
353
- public function index()
354
- {
355
- // ...
356
- }
345
+ .. code-block :: php-annotations
346
+
347
+ use Symfony\Component\Routing\Annotation\Route;
357
348
358
349
/**
359
- * @Route("/{_locale}/posts/{slug}" , name="post ")
350
+ * @Route("/blog", requirements={"locale": "en|es|fr"} , name="blog_ ")
360
351
*/
361
- public function show(Post $post)
352
+ class BlogController
362
353
{
363
- // ...
364
- }
365
- }
366
-
367
- In this example, the route of the ``index() `` action will be called ``blog_index ``
368
- and its URL will be ``/blog/ ``. The route of the ``show() `` action will be called
369
- ``blog_post `` and its URL will be ``/blog/{_locale}/posts/{slug} ``. Both routes
370
- will also validate that the ``_locale `` parameter matches the regular expression
371
- defined in the class annotation.
372
-
373
- The same can be used when importing routes from configuration files:
354
+ /**
355
+ * @Route("/{_locale}", name="index")
356
+ */
357
+ public function index()
358
+ {
359
+ // ...
360
+ }
374
361
375
- .. configuration-block ::
362
+ /**
363
+ * @Route("/{_locale}/posts/{slug}", name="post")
364
+ */
365
+ public function show(Post $post)
366
+ {
367
+ // ...
368
+ }
369
+ }
376
370
377
371
.. code-block :: yaml
378
372
@@ -387,6 +381,9 @@ The same can be used when importing routes from configuration files:
387
381
# these requirements are added to all imported routes
388
382
requirements :
389
383
locale : ' en|es|fr'
384
+ # An imported route with an empty URL will become "/blog/"
385
+ # Uncomment this option to make that URL "/blog" instead
386
+ # trailing_slash_on_root: false
390
387
391
388
.. code-block :: xml
392
389
@@ -397,14 +394,25 @@ The same can be used when importing routes from configuration files:
397
394
xsi : schemaLocation =" http://symfony.com/schema/routing
398
395
https://symfony.com/schema/routing/routing-1.0.xsd" >
399
396
400
- <import resource =" ../src/Controller/" type =" annotation" >
401
- <!-- this is added to the beginning of all imported route URLs -->
402
- <prefix >/blog</prefix >
403
- <!-- this is added to the beginning of all imported route names -->
404
- <name-prefix >blog_</name-prefix >
397
+ <!--
398
+ the 'prefix' value is added to the beginning of all imported route URLs
399
+ the 'name-prefix' value is added to the beginning of all imported route names
400
+ -->
401
+ <import resource =" ../src/Controller/"
402
+ type =" annotation"
403
+ prefix =" /blog"
404
+ name-prefix =" blog_" >
405
405
<!-- these requirements are added to all imported routes -->
406
406
<requirement key =" locale" >en|es|fr</requirement >
407
407
</import >
408
+
409
+ <!-- An imported route with an empty URL will become "/blog/"
410
+ Uncomment this option to make that URL "/blog" instead -->
411
+ <import resource =" ../src/Controller/" type =" annotation"
412
+ prefix =" /blog"
413
+ trailing-slash-on-root =" false" >
414
+ <!-- ... -->
415
+ </import >
408
416
</routes >
409
417
410
418
.. code-block :: php
@@ -416,58 +424,21 @@ The same can be used when importing routes from configuration files:
416
424
$routes->import('../src/Controller/', 'annotation')
417
425
// this is added to the beginning of all imported route URLs
418
426
->prefix('/blog')
427
+ // An imported route with an empty URL will become "/blog/"
428
+ // Pass FALSE as the second argument to make that URL "/blog" instead
429
+ // ->prefix('/blog', false)
419
430
// this is added to the beginning of all imported route names
420
431
->namePrefix('blog_')
421
432
// these requirements are added to all imported routes
422
433
->requirements(['locale' => 'en|es|fr'])
423
434
;
424
435
};
425
436
426
- .. note ::
427
-
428
- If any of the prefixed routes defines an empty path, Symfony adds a trailing
429
- slash to it. In the previous example, an empty path prefixed with ``/site ``
430
- will result in the ``/site/ `` URL. If you want to avoid this behavior, set
431
- the ``trailing_slash_on_root `` option to ``false ``:
432
-
433
- .. configuration-block ::
434
-
435
- .. code-block :: yaml
436
-
437
- # config/routes.yaml
438
- controllers :
439
- resource : ' ../src/Controller/'
440
- type : annotation
441
- prefix : /blog
442
- trailing_slash_on_root : false
443
-
444
- .. code-block :: xml
445
-
446
- <!-- config/routes.xml -->
447
- <?xml version =" 1.0" encoding =" UTF-8" ?>
448
- <routes xmlns =" http://symfony.com/schema/routing"
449
- xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
450
- xsi : schemaLocation =" http://symfony.com/schema/routing
451
- https://symfony.com/schema/routing/routing-1.0.xsd" >
452
-
453
- <import resource =" ../src/Controller/"
454
- type =" annotation"
455
- prefix =" /blog"
456
- trailing-slash-on-root =" false" />
457
- </routes >
458
-
459
- .. code-block :: php
460
-
461
- // config/routes.php
462
- use App\Controller\ArticleController;
463
- use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
464
-
465
- return function (RoutingConfigurator $routes) {
466
- $routes->import('../src/Controller/', 'annotation')
467
- // the second argument is a boolean to configure 'trailing_slash_on_root'
468
- ->prefix('/blog', false)
469
- ;
470
- };
437
+ In this example, the route of the ``index() `` action will be called ``blog_index ``
438
+ and its URL will be ``/blog/ ``. The route of the ``show() `` action will be called
439
+ ``blog_post `` and its URL will be ``/blog/{_locale}/posts/{slug} ``. Both routes
440
+ will also validate that the ``_locale `` parameter matches the regular expression
441
+ defined in the class annotation.
471
442
472
443
.. seealso ::
473
444
0 commit comments