You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: page_creation.rst
+36-39Lines changed: 36 additions & 39 deletions
Original file line number
Diff line number
Diff line change
@@ -94,12 +94,9 @@ to creating a page?
94
94
Annotation Routes
95
95
-----------------
96
96
97
-
.. caution::
98
-
99
-
If you use PHP8 or later, you do not need to install annotations package and instead of annotations, you can use structured metadata with PHP's native syntax (Attributes)
100
-
101
97
Instead of defining your route in YAML, Symfony also allows you to use *annotation*
102
-
routes. To do this, install the annotations package:
98
+
or *attribute* routes. Attributes are built-in in PHP starting from PHP 8. In earlier
99
+
PHP versions you can use annotations. To do this, install the annotations package:
103
100
104
101
.. code-block:: terminal
105
102
@@ -109,42 +106,42 @@ You can now add your route directly *above* the controller:
109
106
110
107
.. configuration-block::
111
108
112
-
.. code-block:: Annotations
113
-
114
-
// src/Controller/LuckyController.php
115
-
116
-
// ...
117
-
+ use Symfony\Component\Routing\Annotation\Route;
118
-
119
-
class LuckyController
120
-
{
121
-
+ /**
122
-
+ * @Route("/lucky/number")
123
-
+ */
124
-
public function number()
125
-
{
126
-
// this looks exactly the same
127
-
}
128
-
}
129
-
130
-
.. code-block:: Attributes
131
-
132
-
// src/Controller/LuckyController.php
133
-
134
-
// ...
135
-
+ use Symfony\Component\Routing\Annotation\Route;
136
-
137
-
class LuckyController
138
-
{
139
-
+ #[Route('/lucky/number')
140
-
public function number()
141
-
{
142
-
// this looks exactly the same
143
-
}
144
-
}
109
+
.. code-block:: php-annotations
110
+
111
+
// src/Controller/LuckyController.php
112
+
113
+
// ...
114
+
+ use Symfony\Component\Routing\Annotation\Route;
115
+
116
+
class LuckyController
117
+
{
118
+
+ /**
119
+
+ * @Route("/lucky/number")
120
+
+ */
121
+
public function number()
122
+
{
123
+
// this looks exactly the same
124
+
}
125
+
}
126
+
127
+
.. code-block:: php-attributes
128
+
129
+
// src/Controller/LuckyController.php
130
+
131
+
// ...
132
+
+ use Symfony\Component\Routing\Annotation\Route;
133
+
134
+
class LuckyController
135
+
{
136
+
+ #[Route('/lucky/number')
137
+
public function number()
138
+
{
139
+
// this looks exactly the same
140
+
}
141
+
}
145
142
146
143
That's it! The page - http://localhost:8000/lucky/number will work exactly
147
-
like before! Annotations are the recommended way to configure routes.
144
+
like before! Annotations/attributes are the recommended way to configure routes.
0 commit comments