Adding JSON response to First Application Tutorial


#1

Hey there, here’s a full on newb question. I’ve worked through the First Application Tutorial successfully, then used what I learned to essentially rebuild it to fit my needs. I’ve really enjoyed how quickly I was able to pull together a simple API. However, for the life of me I can’t figure out how to return a JSON object instead of the standard response object.

I’ve put in some time searching and haven’t been able to figure anything out.

If I wanted to continue the tutorial, but have /tickets respond with a JSON object, what would be the most Slim-like way for me to accomplish that?


#2

Have a look at Returning JSON and see if that helps a bit. You will return the same response object, but Slim offers a withJson helper method.

Also see the source.

$data = [ ];
return $response->withJson($data);