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)

No description

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

at line line 42
__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

at line line 67
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

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

Make easier the creation of JsonResponse from raw json.

Parameters

$data
$status
$headers

at line line 89
$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

at line line 124
$this setJson(string $json)

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

Parameters

string $json

Return Value

$this

Exceptions

InvalidArgumentException

at line line 140
$this setData(mixed $data = array())

Sets the data to be sent as JSON.

Parameters

mixed $data

Return Value

$this

Exceptions

InvalidArgumentException

at line line 163
int getEncodingOptions()

Returns options used while encoding data to JSON.

Return Value

int

at line line 175
$this setEncodingOptions(int $encodingOptions)

Sets options used while encoding data to JSON.

Parameters

int $encodingOptions

Return Value

$this