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.