Return a JSON view explicitly.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 27 Jun 2018 20:38:53 +0000 (22:38 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 27 Jun 2018 20:38:53 +0000 (22:38 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/Controller/PersonController.php

index 806d2124f970c745192e3396e0d2cdd971591735..c6b0b2c12f7321ef4be4e2f68f2ef45a4d1d4008 100644 (file)
@@ -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;
  }
 }