@@ -24,18 +24,22 @@ because it's convenient to put the route and controller in the same place.
24
24
Creating Routes as Attributes or Annotations
25
25
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26
26
27
- On PHP 8, you can use native attributes to configure routes right away. On
28
- PHP 7, where attributes are not available, you can use annotations instead,
29
- provided by the Doctrine Annotations library .
27
+ PHP attributes and annotations allow to define routes next to the code of the
28
+ :doc: ` controllers < /controller >` associated to those routes. Attributes are
29
+ native in PHP 8 and higher versions, so you can use them right away .
30
30
31
- In case you want to use annotations instead of attributes, run this command
32
- once in your application to enable them :
31
+ In PHP 7 and earlier versions you can use annotations (via the Doctrine Annotations
32
+ library), but first you'll need to install the following dependency in your project :
33
33
34
34
.. code-block :: terminal
35
35
36
36
$ composer require doctrine/annotations
37
37
38
- This command also creates the following configuration file:
38
+ Regardless of what you use (attributes or annotations) you need to add a bit of
39
+ configuration to your project before using them. If your project uses
40
+ :ref: `Symfony Flex <symfony-flex >`, this file is already created for you.
41
+ Otherwise, create the following file manually (the ``type: annotation `` option
42
+ also applies to attributes, so you can keep it):
39
43
40
44
.. code-block :: yaml
41
45
@@ -48,8 +52,9 @@ This command also creates the following configuration file:
48
52
resource : ../../src/Kernel.php
49
53
type : annotation
50
54
51
- This configuration tells Symfony to look for routes defined as annotations in
52
- any PHP class stored in the ``src/Controller/ `` directory.
55
+ This configuration tells Symfony to look for routes defined as
56
+ annotations/attributes in any PHP class stored in the ``src/Controller/ ``
57
+ directory.
53
58
54
59
Suppose you want to define a route for the ``/blog `` URL in your application. To
55
60
do so, create a :doc: `controller class </controller >` like the following:
@@ -1385,7 +1390,7 @@ A possible solution is to change the parameter requirements to be more permissiv
1385
1390
1386
1391
// src/Controller/DefaultController.php
1387
1392
namespace App\Controller;
1388
-
1393
+
1389
1394
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1390
1395
use Symfony\Component\HttpFoundation\Response;
1391
1396
use Symfony\Component\Routing\Annotation\Route;
@@ -1505,7 +1510,7 @@ when importing the routes.
1505
1510
1506
1511
// src/Controller/BlogController.php
1507
1512
namespace App\Controller;
1508
-
1513
+
1509
1514
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1510
1515
use Symfony\Component\HttpFoundation\Response;
1511
1516
use Symfony\Component\Routing\Annotation\Route;
0 commit comments