2
votes

I have a problem with injecting Symfony Serializer into the controller. Here is a working example of what behavior I want to achive:

public function someAction(): Response
{
    $goodViews = //here I get an array of Value Objects or DTO's ;

    $serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);

    // normalize gives me a proper array for serialization
    return $this->json(
         $serializer->normalize($goodViews)
    );
}

But now I need to change a direct creation of Serializer with dependency injection to controllers constructor or action. Other way, I think, is to create service which will get ObjectNormalizer and JsonEncoder as arguments, then create a Serializer and then normalize arrays of objects in special method and return result. But I can not figure out how to create serializer in service.yml or describe service dependencies properly. Symfony docs also get simple serializer service or create it manually as I did in the code example.

I thought to get serializer service from a service container in the action (with $this->get('serializer')) or inject it into the controllers constructor with NormalizerInterface (I need to normalize arrays of objects mostly), but this injected serializer fell with such an error:

"message": "Cannot normalize attribute \"options\" because the injected serializer is not a normalizer", "class": "Symfony\Component\Serializer\Exception\LogicException",

so I suppose, that its not configured in a way I've done with manual Serializer creation.

Our version of Symfony is 3.4 Thanks for your attention.

2

2 Answers

0
votes

The decision of my problem is a little bit tricky. ObjectNormalizer was overriden by custom normalizer (with decorate part in the custom service defenition - see https://symfony.com/doc/3.4/service_container/service_decoration.html). Thats why in the framework preconfigured Symfony Serializer I got our custom one, which produced a mistake:

Cannot normalize attribute \"options\" because the injected serializer is not a normalizer

So I've created a new serializer service with ObjectNormalizer:

new_api.serializer_with_object_normalizer:
    class: Symfony\Component\Serializer\Serializer
    arguments:
        0:
            - "@our_custom_serviec_which_override_object_normalizer.inner"
        1:
            - "@serializer.encoder.json"
-1
votes
public function someAction(SerializerInterface $serializer): Response // Add serializer as an argument
{
    $goodViews = //here I get an array of Value Objects or DTO's ;

    // normalize gives me a proper array for serialization
    return $this->json(
         $serializer->normalize($goodViews)
    );
}

In services.yml

# *Bundle/Resources/services.yaml
services:
    YourNamespace/*Bundle/Controller/YourController:
        tags: [ 'controller.service_arguments' ]

Try it and let us know. This should get you going, but there are better ways you can configure your project. Check here for more info on using controllers as services and how to autowire them.

https://symfony.com/doc/3.4/service_container.html

https://symfony.com/doc/3.4/serializer.html