i am building an Api with symfony 4.2 and want to use jms-serializer to serialize my data in Json format, after installing it with
composer require jms/serializer-bundle
and when i try to use it this way :
``` demands = $demandRepo->findAll();
return $this->container->get('serializer')->serialize($demands,'json');```
it gives me this errur :
Service "serializer" not found, the container inside "App\Controller\DemandController" is a smaller service locator that only knows about the "doctrine", "http_kernel", "parameter_bag", "request_stack", "router" and "session" services. Try using dependency injection instead.
Let's say that you have an entity called
Foo.php
that hasid
,name
anddescription
And you would like to return only
id
, andname
when consuming a particular API such asfoo/summary/
in another situation need to returndescription
as wellfoo/details
here's serializer is really helpful.
let's use serializer to get data depends on the group
As I said in my comment, you could use the default serializer of Symfony and use it injecting it by the constructor.
Finally i found the answer using the Symfony serializer it's very easy:
and with it, i don't find any problems with dependencies or DateTime or other problems ;)