Skip to content

Commit f464d5f

Browse files
alexandre-dauboisjaviereguiluz
authored andcommitted
Replace annotations by attributes in Serializer and Controller
1 parent e6c565b commit f464d5f

File tree

4 files changed

+20
-38
lines changed

4 files changed

+20
-38
lines changed

components/property_info.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ SerializerExtractor
418418

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

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

437437
// the `serializer_groups` option must be configured (may be set to null)
438438
$serializerExtractor->getProperties($class, ['serializer_groups' => ['mygroup']]);
439-
439+
440440
If ``serializer_groups`` is set to ``null``, serializer groups metadata won't be
441441
checked but you will get only the properties considered by the Serializer
442442
Component (notably the ``@Ignore`` annotation is taken into account).

controller.rst

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ class::
3333

3434
class LuckyController
3535
{
36-
/**
37-
* @Route("/lucky/number/{max}", name="app_lucky_number")
38-
*/
36+
#[Route('/lucky/number/{max}', name: 'app_lucky_number')]
3937
public function number(int $max): Response
4038
{
4139
$number = random_int(0, $max);
@@ -73,8 +71,8 @@ Mapping a URL to a Controller
7371
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7472

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

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

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

208-
/**
209-
* @Route("/lucky/number/{max}")
210-
*/
206+
#[Route('/lucky/number/{max}')]
211207
public function number(int $max, LoggerInterface $logger): Response
212208
{
213209
$logger->info('We are logging!');

reference/configuration/framework.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2667,7 +2667,7 @@ If this option is enabled, serialization groups can be defined using annotations
26672667

26682668
.. seealso::
26692669

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

26722672
.. _reference-serializer-name_converter:
26732673

serializer.rst

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,10 @@ configuration:
159159
160160
.. _serializer-using-serialization-groups-annotations:
161161

162-
Using Serialization Groups Annotations
163-
--------------------------------------
162+
Using Serialization Groups Attributes
163+
-------------------------------------
164164

165-
To use annotations, first add support for them via the SensioFrameworkExtraBundle:
166-
167-
.. code-block:: terminal
168-
169-
$ composer require sensio/framework-extra-bundle
170-
171-
Next, add the :ref:`@Groups annotations <component-serializer-attributes-groups-annotations>`
165+
You can add :ref:`#[Groups] attributes <component-serializer-attributes-groups-annotations>`
172166
to your class::
173167

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

180-
/**
181-
* @ORM\Entity()
182-
*/
174+
#[ORM\Entity]
183175
class Product
184176
{
185-
/**
186-
* @ORM\Id
187-
* @ORM\GeneratedValue
188-
* @ORM\Column(type="integer")
189-
* @Groups({"show_product", "list_product"})
190-
*/
177+
#[ORM\Id]
178+
#[ORM\GeneratedValue]
179+
#[ORM\Column(type: 'integer')]
180+
#[Groups(['show_product', 'list_product'])]
191181
private $id;
192182

193-
/**
194-
* @ORM\Column(type="string", length=255)
195-
* @Groups({"show_product", "list_product"})
196-
*/
183+
#[ORM\Column(type: 'string', length: 255)]
184+
#[Groups(['show_product', 'list_product'])]
197185
private $name;
198186

199-
/**
200-
* @ORM\Column(type="integer")
201-
* @Groups({"show_product"})
202-
*/
187+
#[ORM\Column(type: 'integer')]
188+
#[Groups(['show_product'])]
203189
private $description;
204190
}
205191

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

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

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

0 commit comments

Comments
 (0)