In Symfony2, is there a way to pretty-print JSON responses? - json

I am writing a REST API in Symfony2, and I have my controller outputting JSON responses using the provided JsonResponse class:
$response = new JsonResponse(null, $status);
$response->setData($node['Content']);
return $response;
However, for debugging purposes, it would be nice to be able to pretty print the output. Is there an argument I can pass to the JsonResponse object to enable pretty-printing of the output?

Creating an event listener for that, as in Pawel's answer is overengineering.
To prettify, you just pass in the JSON_PRETTY_PRINT constant to the JsonResponse via the setEncodingOptions, like in this example:
$response = new JsonResponse( [ 'your' => 'data' ] );
$response->setEncodingOptions( $response->getEncodingOptions() | JSON_PRETTY_PRINT );
return $response;
You can see the Symfony API for more information:
Class JsonResponse:
http://api.symfony.com/3.2/Symfony/Component/HttpFoundation/JsonResponse.html
Method setEncodingOptions:
http://api.symfony.com/3.2/Symfony/Component/HttpFoundation/JsonResponse.html#method_setEncodingOptions
This is documentation for v3.2... but older versions like for example v2.7 also have this method.

You can create EventListener for that. Here is mine
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Preetify json response.
*/
class FormatJsonResponseListener
{
public function onResponse(FilterResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
$request = $event->getRequest();
if (APPLICATION_ENV === 'development' || APPLICATION_ENV === 'dev' || $request->query->get('pretty_json', false) == true) {
$response = $event->getResponse();
$responseData = $event->getResponse()->getContent();
$response->setContent(Json::indent($responseData));
$event->setResponse($response);
}
}
}
And register that service with:
#services.ym
services:
your.service.listener.format_json:
class: Your\Vendor\FormatJsonResponseListener
tags:
- { name: kernel.event_listener, event: kernel.response, method: onResponse }
My JSON class is here: https://github.com/sourcefabric/Newscoop/blob/master/newscoop/library/Newscoop/Gimme/Json.php
Instead APPLICATION_ENV you can pass to listener parameter kernel.debug.
You can also modify response with (PHP >= 5.4.0, HttpFoundation >= 2.5)
$response->setEncodingOptions($response->getEncodingOptions() | JSON_PRETTY_PRINT);

Related

How to retrieve data from MySQL to JSON?

I have a Symfony project where I want to store the all the rows from my MySQL table to JSON. Currently there are five rows in my table, but in my browser it only returns five empty values as {"results":[{},{},{},{},{}]}
I guess I have done something right, but not everything. What am I missing in my code?
#[Route('/budget/api', name: 'budget-api')]
public function index(Request $request, BudgetRepository $repository)
{
$results = $repository->findAll();
return $this->json(['results' => $results]);
}
Try createQueryBuilder its usefull.
#[Route('/budget/api', name: 'budget-api')]
public function index(Request $request, BudgetRepository $repository)
{
$qb = $repository->createQueryBuilder("b");
$results = $qb->getQuery()->getArrayResult();
return $this->json(['results' => $results]);
}
You can use the serializer or re-create the array yourself like that
$courses = $doctrine->getRepository(Course::class)->findByLikeTitle($search, $userId);
foreach ($courses as $key => $course) {
$jsonCourses[$key]['title'] = $course->getTitle();
}
```
You can achieve this by Using a Serializer to convert the array of objects into JSON. There are other ways to achieve this like using jsonResponse for example. But the serializer is the most robust way imo.
Example only:
use Symfony\Component\Serializer\SerializerInterface;
#[Route('/budget/api', name: 'budget-api')]
public function index(Request $request, BudgetRepository $repository, SerializerInterface $serializer)
{
$results = $repository->findAll();
$jsonResults = $serializer->serialize($results, 'json');
//If you need to handle any circular references, you can use this..
$jsonResults = $serializer->serialize($results, 'json', array(
'circular_reference_handler' => function ($object) { return $object; },
));
return $jsonResults;
}

Symfony request with json for postman

I have my CRUD but I need to do a POST request with postman. I've been reading some posts but I don't really understand how it works.
My routing for that:
jugador_create:
path: /{_format}
defaults: { _controller: "FutbolBundle:Jugador:new", _format: html }
requirements: { _method: post, _format: html|xml|json }
My controller is this:
public function newAction(Request $request)
{
$entity = new Jugador();
$form = $this->createCreateForm($entity);
if ($request->getMethod() == 'POST'){
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('jugador_show', array('id' => $entity->getId())));
}}
return $this->render('FutbolBundle:Jugador:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
How do I do it so in postman I can do a JSON post and it creates it? I think I need to do a switch with Json, xml and default case but I don't really understand how to do the Json part.
Thank you so much.
Also, in my index I already did the switch with Json and xml but it's pretty different to a GET than a POST.
public function indexAction(Request $request){
$request = $this->getRequest();
$serializer = new Serializer(array(new GetSetMethodNormalizer()),array(new XmlEncoder(), new JsonEncoder()));
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('FutbolBundle:Jugador')->findAll();
switch ($request->getRequestFormat()){
case "json":
$response=new Response();
$response->setContent($serializer->serialize($entities,'json'));
return $response;
break;
case "xml":
$response=new Response();
$response->setContent($serializer->serialize($entities,'xml'));
return $response;
break;
default:
return $this->render('FutbolBundle:Jugador:index.html.twig', array(
'entities' => $entities,
));
}
}
My sugestion is as follow:
Implemet a format listener to avoid repeating your code in all controllers
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if ('json' === $request->getContentType() && $request->getContent()) {
$data = json_decode($request->getContent(), true);
$request->request->replace($data);
}
}
After that your controller should not change because you already transformed the data from json to regular posted data. You can do the same with the xml format or what ever format you want but html.
Specify the headers in postman
At Headers options below the method/url just add the Content-type header as follow.
Content-type(key) application/json (value)
Content-type(key) application/xml (value)
That should work
Hope it helps.
PD: Si no entiendes todo lo que he escrito hazmelo saber.
Just in case anyone needs help, I actually did it pretty simple using sets and the entity of Symfony.
$data = json_decode($request->getContent(),true);
$entity->setNom($data["nom"]);
$entity->setEquip($data["equip"]);
... (more sets if you need them)
and this for inserting it into the data base.
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();

How to configure Symfony JsonResponse output?

Is there any way to configure JsonResponse output to be - for example - unescaped unicode?
something like the output of this php function:
json_encode($array, JSON_UNESCAPED_UNICODE);
Yes, here you go:
$response = new JsonResponse($data);
$response->setEncodingOptions(JSON_UNESCAPED_UNICODE);
return $response;
To do this globally, make an event listener. Something like this:
services.yml:
event_listeners.json_formatter_listener:
class: EventListeners\JsonResponseFormatterListener
tags:
- { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }
JsonResponseFormatterListener.php:
<?php
namespace EventListeners;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
class JsonResponseFormatterListener
{
public function onKernelResponse(FilterResponseEvent $event)
{
$response = $event->getResponse();
if ($response instanceof JsonResponse) {
$response->setEncodingOptions(JSON_UNESCAPED_UNICODE);
}
}
}

Cakephp3: How can I return json data?

I am having a ajax post call to a cakePhp Controller:
$.ajax({
type: "POST",
url: 'locations/add',
data: {
abbreviation: $(jqInputs[0]).val(),
description: $(jqInputs[1]).val()
},
success: function (response) {
if(response.status === "success") {
// do something with response.message or whatever other data on success
console.log('success');
} else if(response.status === "error") {
// do something with response.message or whatever other data on error
console.log('error');
}
}
});
When I try this I get the following error message:
Controller actions can only return Cake\Network\Response or null.
Within the AppController I have this
$this->loadComponent('RequestHandler');
enabled.
the Controller function looks like this:
public function add()
{
$this->autoRender = false; // avoid to render view
$location = $this->Locations->newEntity();
if ($this->request->is('post')) {
$location = $this->Locations->patchEntity($location, $this->request->data);
if ($this->Locations->save($location)) {
//$this->Flash->success(__('The location has been saved.'));
//return $this->redirect(['action' => 'index']);
return json_encode(array('result' => 'success'));
} else {
//$this->Flash->error(__('The location could not be saved. Please, try again.'));
return json_encode(array('result' => 'error'));
}
}
$this->set(compact('location'));
$this->set('_serialize', ['location']);
}
What do I miss here? Is there any additional settings needed?
Instead of returning the json_encode result, set the response body with that result and return it back.
public function add()
{
$this->autoRender = false; // avoid to render view
$location = $this->Locations->newEntity();
if ($this->request->is('post')) {
$location = $this->Locations->patchEntity($location, $this->request->data);
if ($this->Locations->save($location)) {
//$this->Flash->success(__('The location has been saved.'));
//return $this->redirect(['action' => 'index']);
$resultJ = json_encode(array('result' => 'success'));
$this->response->type('json');
$this->response->body($resultJ);
return $this->response;
} else {
//$this->Flash->error(__('The location could not be saved. Please, try again.'));
$resultJ = json_encode(array('result' => 'error', 'errors' => $location->errors()));
$this->response->type('json');
$this->response->body($resultJ);
return $this->response;
}
}
$this->set(compact('location'));
$this->set('_serialize', ['location']);
}
Edit (credit to #Warren Sergent)
Since CakePHP 3.4, we should use
return $this->response->withType("application/json")->withStringBody(json_encode($result));
Instead of :
$this->response->type('json');
$this->response->body($resultJ);
return $this->response;
CakePHP Documentation
Most answers I've seen here are either outdated, overloaded with unnecessary information, or rely on withBody(), which feels workaround-ish and not a CakePHP way.
Here's what worked for me instead:
$my_results = ['foo'=>'bar'];
$this->set([
'my_response' => $my_results,
'_serialize' => 'my_response',
]);
$this->RequestHandler->renderAs($this, 'json');
More info on RequestHandler. Seemingly it's not getting deprecated anytime soon.
UPDATE: CakePHP 4
$this->set(['my_response' => $my_results]);
$this->viewBuilder()->setOption('serialize', true);
$this->RequestHandler->renderAs($this, 'json');
More info
there are few things to return JSON response:
load RequestHandler component
set rendering mode as json
set content type
set required data
define _serialize value
for example you can move first 3 steps to some method in parent controller class:
protected function setJsonResponse(){
$this->loadComponent('RequestHandler');
$this->RequestHandler->renderAs($this, 'json');
$this->response->type('application/json');
}
later in your controller you should call that method, and set required data;
if ($this->request->is('post')) {
$location = $this->Locations->patchEntity($location, $this->request->data);
$success = $this->Locations->save($location);
$result = [ 'result' => $success ? 'success' : 'error' ];
$this->setJsonResponse();
$this->set(['result' => $result, '_serialize' => 'result']);
}
also it looks like you should also check for request->is('ajax); I'm not sure about returning json in case of GET request, so setJsonResponse method is called within if-post block;
in your ajax-call success handler you should check result field value:
success: function (response) {
if(response.result == "success") {
console.log('success');
}
else if(response.result === "error") {
console.log('error');
}
}
In the latest version of CakePHP $this->response->type() and $this->response->body() are deprecated.
Instead you should use $this->response->withType() and $this->response->withStringBody()
E.g:
(this was pinched from the accepted answer)
if ($this->request->is('post')) {
$location = $this->Locations->patchEntity($location, $this->request->data);
if ($this->Locations->save($location)) {
//$this->Flash->success(__('The location has been saved.'));
//return $this->redirect(['action' => 'index']);
$resultJ = json_encode(array('result' => 'success'));
$this->response = $this->response
->withType('application/json') // Here
->withStringBody($resultJ) // and here
return $this->response;
}
}
Relevant Documentation
When you return JSON data you need to define the data type and response body information like below:
$cardInformation = json_encode($cardData);
$this->response->type('json');
$this->response->body($cardInformation);
return $this->response;
In you case just change this return json_encode(array('result' => 'success')); line with below code:
$responseResult = json_encode(array('result' => 'success'));
$this->response->type('json');
$this->response->body($responseResult);
return $this->response;
RequestHandler is not required to send json.
In controller's action:
$this->viewBuilder()->setClassName('Json');
$result = ['result' => $success ? 'success' : 'error'];
$this->set($result);
$this->set('_serialize', array_keys($result));
As of cakePHP 4.x.x the following should work assuming that your controller and routes are set as shown below:
controller: <your_project_name>/src/Controller/StudentsController.php
public function index()
{
$students = $this->Students->find('all');
$this->set(compact('students'));
$this->viewBuilder()->setOption('serialize',['students']);
}
Routes: <your_project_name>/config/routes.php
<?php
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
/** #var \Cake\Routing\RouteBuilder $routes */
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
$builder->setExtensions(['json']);
$builder->resources('Students');
$builder->fallbacks();
});
Run bin/cake server and visit http://localhost:8765/students.json using postman/insomnia or just the normal browser.
See further documentation for setting up Restful controllers and Restful Routing
Don't forget to set the method to GET on postman and insomnia.
Though I'm not a CakePHP Guru, in my case i'm using cake > 4 and I need some results by ajax call. For this, from my controller i wrote,
echo json_encode(Dashboard::recentDealers()); die;
and in my JS file i just need to parse the data using
JSON.parse(data)
The ajax call like
$.get('/recent-dealers', function (data, status) {
console.log (JSON.parse(data)); });
});

How to return Repository Objects as Json on Symfony2

I'm trying to return the users like this, but of course it doesn't work, I need the data as JSon since im working with BackboneJs
/**
* #Route("/mytest",name="ajax_user_path")
*/
public function ajaxAction()
{
$em = $this->get('doctrine')->getManager();
$users = $this->get('doctrine')->getRepository('GabrielUserBundle:Fosuser')->findAll();
$response = array("users"=>$users);
return new Response(json_encode($response));
}
Thanks for your help guys, here is the Solution
Get the JMSSerializerBundle,
This is the code on the controller
/**
* #Route("/user")
* #Template()
*/
public function userAction()
{
$em = $this->get('doctrine')->getManager();
$users = $this->get('doctrine')->getRepository('GabrielUserBundle:Fosuser')->findAll();
$serializer = $this->get('jms_serializer');
$response = $serializer->serialize($users,'json');
return new Response($response);
}
So, findAll returns an array of entities (objects) and json_encode cannot correctly encode that array. You have to prepare your data berofe send response like that:
Example:
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* #Route("/mytest",name="ajax_user_path")
*/
public function ajaxAction()
{
$users = $this->get('doctrine')->getRepository('GabrielUserBundle:Fosuser')->findAll();
$response = array();
foreach ($users as $user) {
$response[] = array(
'user_id' => $user->getId(),
// other fields
);
}
return new JsonResponse(json_encode($response));
}
Moreover, it would be great if you put preparing response to ex. UserRepository class.
With Symfony you have JsonResponse like :
return new JsonResponse($users);
And don't forget to add the header :
use Symfony\Component\HttpFoundation\JsonResponse;
I have never tried to encode a complete object, but I have used json with arrays of informations like this:
$vars = array(
'test' => 'test'
);
$response = new JsonResponse($vars);
return $response;
As you can see in JsonResponse, its function setData() is encoding the array, so you don't have to do it yourself:
public function setData($data = array())
{
// Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML.
$this->data = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
return $this->update();
}