From: Jérôme Benoit Date: Wed, 27 Jun 2018 20:38:53 +0000 (+0200) Subject: Return a JSON view explicitly. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=5347d06b101fb6f14d4ffef9464177408200b1ff;p=Project_proches_de_moi-server.git Return a JSON view explicitly. Signed-off-by: Jérôme Benoit --- diff --git a/src/Controller/PersonController.php b/src/Controller/PersonController.php index 806d212..c6b0b2c 100644 --- a/src/Controller/PersonController.php +++ b/src/Controller/PersonController.php @@ -4,6 +4,8 @@ namespace App\Controller; use App\Entity\Person; use FOS\RestBundle\Controller\FOSRestController; use FOS\RestBundle\Controller\Annotations as Rest; +use FOS\RestBundle\View\ViewHandler; +use FOS\RestBundle\View\View; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -98,23 +100,26 @@ class PersonController extends FOSRestController } /** - * @Rest\View() * @Rest\Get( * path = "/api/person/{id}", * name = "show_person", * requirements = {"id"="\d+"} * ) + * @Rest\View() */ public function showPerson(Request $request) { - $em = $this->getDoctrine()->getManager(); - $person = $em->getRepository('App:Person')->find($request->get('id')); + $em = $this->getDoctrine()->getManager(); + $person = $em->getRepository('App:Person')->find($request->get('id')); - if (empty($person)) { + if (empty($person)) { return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); } - return $person; + $view = View::create($person); + $view->setFormat('json'); + + return $view; } /** @@ -127,14 +132,17 @@ class PersonController extends FOSRestController */ public function showPersonByEmail(Request $request) { - $em = $this->getDoctrine()->getManager(); - $person = $em->getRepository('App:Person')->find($request->get('email')); + $em = $this->getDoctrine()->getManager(); + $person = $em->getRepository('App:Person')->find($request->get('email')); + + if (empty($person)) { + return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); + } - if (empty($person)) { - return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); - } + $view = View::create($person); + $view->setFormat('json'); - return $person; + return $view; } /** @@ -147,14 +155,17 @@ class PersonController extends FOSRestController */ public function showPersonFriends(Request $request) { - $em = $this->getDoctrine()->getManager(); - $person = $em->getRepository('App:Person')->find($request->get('id')); + $em = $this->getDoctrine()->getManager(); + $person = $em->getRepository('App:Person')->find($request->get('id')); + + if (empty($person)) { + return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); + } - if (empty($person)) { - return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); - } + $view = View::create($person->getFriends()); + $view->setFormat('json'); - return $person->getFriends(); + return $view; } /** @@ -167,13 +178,16 @@ class PersonController extends FOSRestController */ public function showPersonFriendsByEmail(Request $request) { - $em = $this->getDoctrine()->getManager(); - $person = $em->getRepository('App:Person')->find($request->get('email')); + $em = $this->getDoctrine()->getManager(); + $person = $em->getRepository('App:Person')->find($request->get('email')); + + if (empty($person)) { + return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); + } - if (empty($person)) { - return new JsonResponse(['message' => 'Person not found'], Response::HTTP_NOT_FOUND); - } + $view = View::create($person->getFriends()); + $view->setFormat('json'); - return $person->getFriends(); + return $view; } }