Skip to content

[Serializer] update doc for 3.4 about normalizer #10543

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 24, 2018
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
50 changes: 10 additions & 40 deletions serializer/custom_normalizer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ process. For that you'll have to create your own normalizer. But it's usually
preferable to let Symfony normalize the object, then hook into the normalization
to customize the normalized data. To do that, leverage the ``ObjectNormalizer``::

namespace AppBundle\Serializer;
namespace App\Serializer;

use AppBundle\Entity\Topic;
use App\Entity\Topic;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
Expand Down Expand Up @@ -53,45 +53,15 @@ to customize the normalized data. To do that, leverage the ``ObjectNormalizer``:
}
}

Registering it in Your Application
----------------------------------

In order to enable the normalizer in an application based on the entire Symfony
framework, you must register it as a service and :doc:`tag it </service_container/tags>`
with ``serializer.normalizer``.

.. configuration-block::

.. code-block:: yaml

# app/config/services.yml
services:
app.yaml_encoder:
class: AppBundle\Serializer\TopicNormalizer
tags:
- { name: serializer.normalizer }

.. code-block:: xml

<!-- app/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="app.yaml_encoder" class="AppBundle\Serializer\TopicNormalizer">
<tag name="serializer.normalizer" />
</service>
</services>
</container>
Registering it in your Application
----------------------------------

.. code-block:: php
If you use the Symfony Framework. then you probably want to register this normalizer
as a service in your app. If you're using the :ref:`default services.yml configuration <service-container-services-load-example>`,
that's done automatically!

// app/config/services.php
use AppBundle\Serializer\TopicNormalizer;
.. tip::

$container
->register('app.yaml_encoder', TopicNormalizer::class)
->addTag('serializer.normalizer')
;
If you're not using autoconfigure, make sure to register your class as a service
and tag it with ``serializer.normalizer``.