JsonResponse

class JsonResponse extends Response

Response represents an HTTP response in JSON format.

Note that this class does not force the returned JSON content to be an object. It is however recommended that you do return an object as it protects yourself against XSSI and JSON-JavaScript Hijacking.

Constants

DEFAULT_ENCODING_OPTIONS

Methods

__construct(mixed $data = null, int $status = 200, array $headers = array(), bool $json = false)
static JsonResponse create(mixed $data = null, int $status = 200, array $headers = array())

Factory method for chainability.

static fromJsonString($data = null, $status = 200, $headers = array())

Make easier the creation of JsonResponse from raw json.

$this setCallback(string|null $callback = null)

Sets the JSONP callback.

$this setJson(string $json)

Sets a raw string containing a JSON document to be sent.

$this setData(mixed $data = array())

Sets the data to be sent as JSON.

int getEncodingOptions()

Returns options used while encoding data to JSON.

$this setEncodingOptions(int $encodingOptions)

Sets options used while encoding data to JSON.

Details

__construct(mixed $data = null, int $status = 200, array $headers = array(), bool $json = false)

Parameters

mixed $data The response data
int $status The response status code
array $headers An array of response headers
bool $json If the data is already a JSON string

static JsonResponse create(mixed $data = null, int $status = 200, array $headers = array())

Factory method for chainability.

Example:

return JsonResponse::create($data, 200)
    ->setSharedMaxAge(300);

Parameters

mixed $data The json response data
int $status The response status code
array $headers An array of response headers

Return Value

JsonResponse

static fromJsonString($data = null, $status = 200, $headers = array())

Make easier the creation of JsonResponse from raw json.

Parameters

$data
$status
$headers

$this setCallback(string|null $callback = null)

Sets the JSONP callback.

Parameters

string|null $callback The JSONP callback or null to use none

Return Value

$this

Exceptions

InvalidArgumentException When the callback name is not valid

$this setJson(string $json)

Sets a raw string containing a JSON document to be sent.

Parameters

string $json

Return Value

$this

Exceptions

InvalidArgumentException

$this setData(mixed $data = array())

Sets the data to be sent as JSON.

Parameters

mixed $data

Return Value

$this

Exceptions

InvalidArgumentException

int getEncodingOptions()

Returns options used while encoding data to JSON.

Return Value

int

$this setEncodingOptions(int $encodingOptions)

Sets options used while encoding data to JSON.

Parameters

int $encodingOptions

Return Value

$this

© 2004–2017 Fabien Potencier
Licensed under the MIT License.
http://api.symfony.com/4.0/Symfony/Component/HttpFoundation/JsonResponse.html