I am trying return a JSON response from a controller in Symfony 2. Form example, in Spring MVC I can get a JSON response with @ResponseBody annotattion. I want get a JSON re
You need to do this (based on previous answer):
public function getAllCategoryAction() {
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT c
FROM AppBundle:Categoria c'
);
$categorias = $query->getArrayResult();
$response = new Response(json_encode($categorias));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
It works perfect with any Query that Doctrine returns as array.