class JsonResponse extends JsonResponse (View source)

Traits

Properties

mixed $original

The original content of the response.

from  ResponseTrait
Throwable|null $exception

The exception that triggered the error response (if applicable).

from  ResponseTrait
static protected array $macros

The registered string macros.

from  Macroable

Methods

int
status()

Get the status code for the response.

string
statusText()

Get the status text for the response.

string
content()

Get the content of the response.

mixed
getOriginalContent()

Get the original response content.

$this
header(string $key, array|string $values, bool $replace = true)

Set a header on the Response.

$this
withHeaders(HeaderBag|array $headers)

Add an array of headers to the response.

$this
cookie(Cookie|mixed $cookie)

Add a cookie to the response.

$this
withCookie(Cookie|mixed $cookie)

Add a cookie to the response.

$this
withoutCookie(Cookie|mixed $cookie, string|null $path = null, string|null $domain = null)

Expire a cookie when sending the response.

string|null
getCallback()

Get the callback of the response.

$this
withException(Throwable $e)

Set the exception to attach to the response.

never
throwResponse()

Throws the response in a HttpResponseException instance.

static void
macro(string $name, object|callable $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Checks if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically handle calls to the class.

void
__construct(mixed $data = null, int $status = 200, array $headers = [], int $options = 0, bool $json = false)

Create a new JSON response instance.

static JsonResponse
fromJsonString(string|null $data = null, int $status = 200, array $headers = [])

{@inheritdoc}

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

Sets the JSONP callback.

mixed
getData(bool $assoc = false, int $depth = 512)

Get the json_decoded data from the response.

setData($data = [])

{@inheritdoc}

bool
hasValidJson(int $jsonError)

Determine if an error occurred during JSON encoding.

setEncodingOptions($options)

{@inheritdoc}

bool
hasEncodingOption(int $option)

Determine if a JSON encoding option is set.

Details

int status()

Get the status code for the response.

Return Value

int

string statusText()

Get the status text for the response.

Return Value

string

string content()

Get the content of the response.

Return Value

string

mixed getOriginalContent()

Get the original response content.

Return Value

mixed

$this header(string $key, array|string $values, bool $replace = true)

Set a header on the Response.

Parameters

string $key
array|string $values
bool $replace

Return Value

$this

$this withHeaders(HeaderBag|array $headers)

Add an array of headers to the response.

Parameters

HeaderBag|array $headers

Return Value

$this

Add a cookie to the response.

Parameters

Cookie|mixed $cookie

Return Value

$this

$this withCookie(Cookie|mixed $cookie)

Add a cookie to the response.

Parameters

Cookie|mixed $cookie

Return Value

$this

$this withoutCookie(Cookie|mixed $cookie, string|null $path = null, string|null $domain = null)

Expire a cookie when sending the response.

Parameters

Cookie|mixed $cookie
string|null $path
string|null $domain

Return Value

$this

string|null getCallback()

Get the callback of the response.

Return Value

string|null

$this withException(Throwable $e)

Set the exception to attach to the response.

Parameters

Throwable $e

Return Value

$this

never throwResponse()

Throws the response in a HttpResponseException instance.

Return Value

never

Exceptions

HttpResponseException

static void macro(string $name, object|callable $macro)

Register a custom macro.

Parameters

string $name
object|callable $macro

Return Value

void

static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

static bool hasMacro(string $name)

Checks if macro is registered.

Parameters

string $name

Return Value

bool

static void flushMacros()

Flush the existing macros.

Return Value

void

static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

void __construct(mixed $data = null, int $status = 200, array $headers = [], int $options = 0, bool $json = false)

Create a new JSON response instance.

Parameters

mixed $data
int $status
array $headers
int $options
bool $json

Return Value

void

static JsonResponse fromJsonString(string|null $data = null, int $status = 200, array $headers = [])

{@inheritdoc}

Parameters

string|null $data
int $status
array $headers

Return Value

JsonResponse

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

Sets the JSONP callback.

Parameters

string|null $callback

Return Value

$this

mixed getData(bool $assoc = false, int $depth = 512)

Get the json_decoded data from the response.

Parameters

bool $assoc
int $depth

Return Value

mixed

JsonResponse setData($data = [])

{@inheritdoc}

Parameters

$data

Return Value

JsonResponse

protected bool hasValidJson(int $jsonError)

Determine if an error occurred during JSON encoding.

Parameters

int $jsonError

Return Value

bool

JsonResponse setEncodingOptions($options)

{@inheritdoc}

Parameters

$options

Return Value

JsonResponse

bool hasEncodingOption(int $option)

Determine if a JSON encoding option is set.

Parameters

int $option

Return Value

bool