Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Symfony Response Object that creates a JSON object which envelopes your content in a "content" part and has "meta" data that you can add on to. I tuses the JMSSerializerBundle to make sure it serializes properly.
* User: sarel
* Date: 2015/05/31
* Time: 10:10 AM
namespace AH\AQueueBundle\Helper;
use JMS\Serializer\SerializerBuilder;
use Symfony\Component\HttpFoundation\Response;
class ResponseJSON extends Response
* Constructor.
* @param mixed $content The response content, see setContent()
* @param int $status The response status code
* @param array $headers An array of response headers
* @throws \InvalidArgumentException When the HTTP status code is not valid
* @api
public function __construct($content = '', $status = 200, $headers = array(), $extra_meta = array())
parent::__construct('', 200, array('Content-Type' => 'application/json'));
$meta = array_merge(array('status'=>$status), $extra_meta);
$serializer = SerializerBuilder::create()->build();
$json = $serializer->serialize(
'content' => $content,
'meta' => $meta
), 'json');

This comment has been minimized.

Copy link
Owner Author

@sarelvdwalt sarelvdwalt commented Jun 1, 2015


$content = array('foo'=>'bar');
$extraMeta = array('tell_the_user'=>'something');
return new ResponseJSON($content, Response::HTTP_OK, null, $extraMeta);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.