class JsonResponse extends Response

响应JSON

$response = new JsonResponse();
$response->setStatusCode(200);
$response->setData(['data' => 123]);

$response->send();

Traits

ContainerAwareTrait

Methods

setContainer(ContainerInterface $container)

设置依赖注入容器

getContainer()

获取依赖注入容器

__construct(mixed $data = null, int $code = 200, string $message = null)

JsonResponse constructor.

$this
setStatusCode(int $code, string $message = null)

设置响应状态

from Response
getContentType()

获取响应类型

from Response
$this
setContentType(string $contentType, string $charset = null)

设置响应类型

from Response
string|null
getContent()

获取响应内容

from Response
$this
setContent(string $content = null)

设置响应内容

from Response
array
getCookies()

获取响应的 cookies 信息

from Response
$this
setCookie(array $cookie)

设置响应的 cookie 信息

from Response
array
getHeaders()

获取响应的头信息

from Response
$this
setHeader(string $header, string $value = null)

设置响应头信息

from Response
$this
redirect(string $location = null, int $code = 302)

跳转

from Response
$this
send()

发送响应数据

from Response
$this
sendContent()

发送响应内容

from Response
$this
sendCookies()

发送响应 cookie

from Response
$this
sendHeaders()

发送响应头

from Response
$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)

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

in ContainerAwareTrait at line 22
setContainer(ContainerInterface $container)

设置依赖注入容器

Parameters

ContainerInterface $container 容器对象实例

in ContainerAwareTrait at line 32
ContainerInterface getContainer()

获取依赖注入容器

Return Value

ContainerInterface

at line 35
__construct(mixed $data = null, int $code = 200, string $message = null)

JsonResponse constructor.

Parameters

mixed $data
int $code 状态码
string $message 状态描述

in Response at line 102
$this setStatusCode(int $code, string $message = null)

设置响应状态

Parameters

int $code 状态码
string $message 状态描述

Return Value

$this

in Response at line 113
getContentType()

获取响应类型

in Response at line 130
$this setContentType(string $contentType, string $charset = null)

设置响应类型

$response->setContentType('application/javascript');

Parameters

string $contentType
string $charset

Return Value

$this

in Response at line 147
string|null getContent()

获取响应内容

Return Value

string|null

in Response at line 159
$this setContent(string $content = null)

设置响应内容

Parameters

string $content

Return Value

$this

in Response at line 171
array getCookies()

获取响应的 cookies 信息

Return Value

array

in Response at line 183
$this setCookie(array $cookie)

设置响应的 cookie 信息

Parameters

array $cookie 单个 cookie 信息

Return Value

$this

in Response at line 206
array getHeaders()

获取响应的头信息

Return Value

array

in Response at line 219
$this setHeader(string $header, string $value = null)

设置响应头信息

Parameters

string $header
string $value

Return Value

$this

in Response at line 236
$this redirect(string $location = null, int $code = 302)

跳转

Parameters

string $location 跳转地址
int $code 状态码,默认 302 临时重定向

Return Value

$this

in Response at line 261
$this send()

发送响应数据

Return Value

$this

in Response at line 279
$this sendContent()

发送响应内容

Return Value

$this

in Response at line 291
$this sendCookies()

发送响应 cookie

Return Value

$this

in Response at line 313
$this sendHeaders()

发送响应头

Return Value

$this

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

at line 67
$this setJson(string $json)

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

Parameters

string $json

Return Value

$this

at line 81
$this setData(mixed $data)

Sets the data to be sent as JSON.

Parameters

mixed $data

Return Value

$this

at line 93
int getEncodingOptions()

Returns options used while encoding data to JSON.

Return Value

int

at line 105
$this setEncodingOptions(int $encodingOptions)

Sets options used while encoding data to JSON.

Parameters

int $encodingOptions

Return Value

$this