Skip to content

Commit

Permalink
Replace annotations by attributes in Serializer and Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois authored and javiereguiluz committed Aug 9, 2022
1 parent e6c565b commit f464d5f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 38 deletions.
4 changes: 2 additions & 2 deletions components/property_info.rst
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ SerializerExtractor

This extractor depends on the `symfony/serializer`_ library.

Using :ref:`groups metadata <serializer-using-serialization-groups-annotations>`
Using :ref:`groups metadata <serializer-using-serialization-groups-attributes>`
from the :doc:`Serializer component </components/serializer>`,
the :class:`Symfony\\Component\\PropertyInfo\\Extractor\\SerializerExtractor`
provides list information. This extractor is *not* registered automatically
Expand All @@ -436,7 +436,7 @@ with the ``property_info`` service in the Symfony Framework::

// the `serializer_groups` option must be configured (may be set to null)
$serializerExtractor->getProperties($class, ['serializer_groups' => ['mygroup']]);

If ``serializer_groups`` is set to ``null``, serializer groups metadata won't be
checked but you will get only the properties considered by the Serializer
Component (notably the ``@Ignore`` annotation is taken into account).
Expand Down
12 changes: 4 additions & 8 deletions controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ class::

class LuckyController
{
/**
* @Route("/lucky/number/{max}", name="app_lucky_number")
*/
#[Route('/lucky/number/{max}', name: 'app_lucky_number')]
public function number(int $max): Response
{
$number = random_int(0, $max);
Expand Down Expand Up @@ -73,8 +71,8 @@ Mapping a URL to a Controller
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In order to *view* the result of this controller, you need to map a URL to it via
a route. This was done above with the ``@Route("/lucky/number/{max}")``
:ref:`route annotation <annotation-routes>`.
a route. This was done above with the ``#[Route('/lucky/number/{max}')]``
:ref:`route attribute <annotation-routes>`.

To see your page, go to this URL in your browser: http://localhost:8000/lucky/number/100

Expand Down Expand Up @@ -205,9 +203,7 @@ If you need a service in a controller, type-hint an argument with its class
use Symfony\Component\HttpFoundation\Response;
// ...

/**
* @Route("/lucky/number/{max}")
*/
#[Route('/lucky/number/{max}')]
public function number(int $max, LoggerInterface $logger): Response
{
$logger->info('We are logging!');
Expand Down
2 changes: 1 addition & 1 deletion reference/configuration/framework.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2667,7 +2667,7 @@ If this option is enabled, serialization groups can be defined using annotations

.. seealso::

For more information, see :ref:`serializer-using-serialization-groups-annotations`.
For more information, see :ref:`serializer-using-serialization-groups-attributes`.

.. _reference-serializer-name_converter:

Expand Down
40 changes: 13 additions & 27 deletions serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,10 @@ configuration:
.. _serializer-using-serialization-groups-annotations:

Using Serialization Groups Annotations
--------------------------------------
Using Serialization Groups Attributes
-------------------------------------

To use annotations, first add support for them via the SensioFrameworkExtraBundle:

.. code-block:: terminal
$ composer require sensio/framework-extra-bundle
Next, add the :ref:`@Groups annotations <component-serializer-attributes-groups-annotations>`
You can add :ref:`#[Groups] attributes <component-serializer-attributes-groups-annotations>`
to your class::

// src/Entity/Product.php
Expand All @@ -177,29 +171,21 @@ to your class::
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ORM\Entity()
*/
#[ORM\Entity]
class Product
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"show_product", "list_product"})
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups(['show_product', 'list_product'])]
private $id;

/**
* @ORM\Column(type="string", length=255)
* @Groups({"show_product", "list_product"})
*/
#[ORM\Column(type: 'string', length: 255)]
#[Groups(['show_product', 'list_product'])]
private $name;

/**
* @ORM\Column(type="integer")
* @Groups({"show_product"})
*/
#[ORM\Column(type: 'integer')]
#[Groups(['show_product'])]
private $description;
}

Expand All @@ -215,7 +201,7 @@ You can now choose which groups to use when serializing::

The value of the ``groups`` key can be a single string, or an array of strings.

In addition to the ``@Groups`` annotation, the Serializer component also
In addition to the ``#[Groups]`` attribute, the Serializer component also
supports YAML or XML files. These files are automatically loaded when being
stored in one of the following locations:

Expand Down

0 comments on commit f464d5f

Please sign in to comment.