-2

When trying to make a function that serializes an object, turn it into a json and send it as a http response I get the following error:

ContextErrorException

Runtime Notice: Declaration of AppBundle\Controller\DefaultController::json() should be compatible with Symfony\Bundle\FrameworkBundle\Controller\Controller::json($data, $status = 200, $headers = Array, $context = Array)

I was able to show an array of users via var_dump(); before I tried to serialize the object and use it in a function. Here's my code:

<?php

 namespace AppBundle\Controller;

 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Serializer\Serializer;
 use Symfony\Component\Serializer\Encoder\XmlEncoder;
 use Symfony\Component\Serializer\Encoder\JsonEncoder;
 use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;

 class DefaultController extends Controller
{

 public function indexAction(Request $request)
 {
   // replace this example code with whatever you need
   return $this->render('default/index.html.twig', [
   'base_dir'  =>realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
 ]);
}


 public function pruebasAction(Request $request)
 {
    $em =  $this->getDoctrine()->getManager();
    $users = $em->getRepository('BackendBundle:User')->findAll();

    return $this->json($users);

 }

 public function json($data){
    $normalizers = array(new ObjectNormalizer());
    $encoders = array(new JsonEncoder());
    $serializer = new Serializer($normalizers, $encoders);
    $json = $serializer->serialize($data, 'json');

    $response = new Response($json);
    $response->headers->set('Content-Type', 'application/json');

    return $response;

 }
}

What does the "public function json($data){ }" do to be messing up with the program?
and how do I make my json compatible with the one in the framework?

P.D.: Be patience. I'm new with the framework

fortu_42
  • 1
  • 4
  • 1
    Possible duplicate of [Declaration of Methods should be Compatible with Parent Methods in PHP](https://stackoverflow.com/questions/3115388/declaration-of-methods-should-be-compatible-with-parent-methods-in-php) – iainn Sep 26 '17 at 16:54
  • Btw you have a typo, `appplication/json` should be `application/json` – Jason Roman Sep 26 '17 at 19:36

1 Answers1

-1

I just changed the name of the last public function (json for jsons) and it worked, it gave me a readable Json output:

<?php

  namespace AppBundle\Controller;

  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\Response;
  use Symfony\Component\Serializer\Serializer;
  use Symfony\Component\Serializer\Encoder\XmlEncoder;
  use Symfony\Component\Serializer\Encoder\JsonEncoder;
  use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;

  class DefaultController extends Controller
   {

   public function indexAction(Request $request)
    {
      // replace this example code with whatever you need
      return $this->render('default/index.html.twig', [
      'base_dir'   =>realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
      ]);
    }


   public function pruebasAction(Request $request)
    {
     $em =  $this->getDoctrine()->getManager();
     $users = $em->getRepository('BackendBundle:User')->findAll();

     return $this->jsons($users);

     }

     public function jsons($data){
       $normalizers = array(new ObjectNormalizer());
       $encoders = array(new JsonEncoder());
       $serializer = new Serializer($normalizers, $encoders);
       $json = $serializer->serialize($data, 'json');

       $response = new Response($json);
       $response->headers->set('Content-Type', 'application/json');

       return $response;

       }
    }
fortu_42
  • 1
  • 4