diff --git a/page_creation.rst b/page_creation.rst index 642d1dd2de8..7e17c017e9b 100644 --- a/page_creation.rst +++ b/page_creation.rst @@ -94,6 +94,10 @@ to creating a page? Annotation Routes ----------------- +.. caution:: + + 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) + Instead of defining your route in YAML, Symfony also allows you to use *annotation* routes. To do this, install the annotations package: @@ -103,23 +107,41 @@ routes. To do this, install the annotations package: You can now add your route directly *above* the controller: -.. code-block:: diff - - // src/Controller/LuckyController.php - - // ... - + use Symfony\Component\Routing\Annotation\Route; - - class LuckyController - { - + /** - + * @Route("/lucky/number") - + */ - public function number() - { - // this looks exactly the same - } - } +.. configuration-block:: + + .. code-block:: Annotations + + // src/Controller/LuckyController.php + + // ... + + use Symfony\Component\Routing\Annotation\Route; + + class LuckyController + { + + /** + + * @Route("/lucky/number") + + */ + public function number() + { + // this looks exactly the same + } + } + + .. code-block:: Attributes + + // src/Controller/LuckyController.php + + // ... + + use Symfony\Component\Routing\Annotation\Route; + + class LuckyController + { + + #[Route('/lucky/number') + public function number() + { + // this looks exactly the same + } + } That's it! The page - http://localhost:8000/lucky/number will work exactly like before! Annotations are the recommended way to configure routes.