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
minor #16006 Add Attributes code to the 'Annotation Routes' section (BahmanMD)
This PR was merged into the 5.3 branch.
Discussion
----------
Add Attributes code to the 'Annotation Routes' section
Add Attributes code to the 'Annotation Routes' section and update `page_creation.rst`
Commits
-------
44ffc2f Add Attributes code
Copy file name to clipboardExpand all lines: page_creation.rst
+39-17Lines changed: 39 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -94,6 +94,10 @@ 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
+
97
101
Instead of defining your route in YAML, Symfony also allows you to use *annotation*
98
102
routes. To do this, install the annotations package:
99
103
@@ -103,23 +107,41 @@ routes. To do this, install the annotations package:
103
107
104
108
You can now add your route directly *above* the controller:
105
109
106
-
.. code-block:: diff
107
-
108
-
// src/Controller/LuckyController.php
109
-
110
-
// ...
111
-
+ use Symfony\Component\Routing\Annotation\Route;
112
-
113
-
class LuckyController
114
-
{
115
-
+ /**
116
-
+ * @Route("/lucky/number")
117
-
+ */
118
-
public function number()
119
-
{
120
-
// this looks exactly the same
121
-
}
122
-
}
110
+
.. configuration-block::
111
+
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
+
}
123
145
124
146
That's it! The page - http://localhost:8000/lucky/number will work exactly
125
147
like before! Annotations are the recommended way to configure routes.
0 commit comments