Reputation: 872
I'm trying to encapsulate data from my symfony api.
There is my serialization.yml:
AppBundle\Entity\Parcs:
attributes:
id:
groups: ['parcs']
latitude:
groups: ['parcs']
longitude:
groups: ['parcs']
sac:
groups: ['parcs']
name:
groups: ['parcs']
And there is my ParcsController
:
/**
* @Rest\View(serializerGroups={"parcs"})
* @Rest\Get("/parcs")
*/
public function getParcsAction(Request $request)
{
$parcs = $this->get('doctrine.orm.entity_manager')
->getRepository('AppBundle:Parcs')
->findAll();
/* @var $parcs Parc[] */
return $parcs;
}
With those data i want to get [id, sac, name, Coordinate[latitude, longitude]]
.
PS : I'm still learning symfony if you have a good tutorial I'll be happy too. Thx a lot!
Upvotes: 0
Views: 550
Reputation: 946
You've only defined groups there, but you haven't actually 'used' them with the serializer. You need to install and configure Serializer Component https://symfony.com/doc/current/components/serializer.html
Next, you'd want to extend AbstractController
which has a neat json()
method that will automatically figure out that the serializer
service is loaded and will automatically serialize your data.
// extends AbstractController
/**
* @Rest\View(serializerGroups={"parcs"})
* @Rest\Get("/parcs")
*/
public function getParcsAction(Request $request)
{
... everything you said
return $this->json($parcs);
}
EDIT:
Also, I see you're expecting latitude
and longitude
to be a part of different object (from your response example). If that's the case, you need to define those two properties as part of that new object (serialization.yml
). They can't be part of Parcs
entity, you'd need to create a new one.
Upvotes: 1