Skip to content

Commit 09b05b3

Browse files
committed
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
2 parents 7e19b44 + 44ffc2f commit 09b05b3

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

page_creation.rst

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ to creating a page?
9494
Annotation Routes
9595
-----------------
9696

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+
97101
Instead of defining your route in YAML, Symfony also allows you to use *annotation*
98102
routes. To do this, install the annotations package:
99103

@@ -103,23 +107,41 @@ routes. To do this, install the annotations package:
103107
104108
You can now add your route directly *above* the controller:
105109

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+
}
123145

124146
That's it! The page - http://localhost:8000/lucky/number will work exactly
125147
like before! Annotations are the recommended way to configure routes.

0 commit comments

Comments
 (0)