Skip to content

Add Attributes code to the 'Annotation Routes' section #16006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions page_creation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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.
Expand Down