mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 22:32:40 +08:00
Update runtime-responses.md
This commit is contained in:
@ -101,11 +101,11 @@ $response->data = ['message' => 'hello world'];
|
|||||||
Yii支持以下可直接使用的格式,每个实现了[[yii\web\ResponseFormatterInterface|formatter]] 类,
|
Yii支持以下可直接使用的格式,每个实现了[[yii\web\ResponseFormatterInterface|formatter]] 类,
|
||||||
可自定义这些格式器或通过配置 [[yii\web\Response::formatters]] 属性来增加格式器。
|
可自定义这些格式器或通过配置 [[yii\web\Response::formatters]] 属性来增加格式器。
|
||||||
|
|
||||||
* [[yii\web\Response::FORMAT_HTML|HTML]]: 通过 [[yii\web\HtmlResponseFormatter]] 来实现.
|
* [[yii\web\Response::FORMAT_HTML|HTML]]:通过 [[yii\web\HtmlResponseFormatter]] 来实现。
|
||||||
* [[yii\web\Response::FORMAT_XML|XML]]: 通过 [[yii\web\XmlResponseFormatter]]来实现.
|
* [[yii\web\Response::FORMAT_XML|XML]]:通过 [[yii\web\XmlResponseFormatter]] 来实现。
|
||||||
* [[yii\web\Response::FORMAT_JSON|JSON]]: 通过 [[yii\web\JsonResponseFormatter]]来实现.
|
* [[yii\web\Response::FORMAT_JSON|JSON]]:通过 [[yii\web\JsonResponseFormatter]] 来实现。
|
||||||
* [[yii\web\Response::FORMAT_JSONP|JSONP]]: 通过 [[yii\web\JsonResponseFormatter]]来实现.
|
* [[yii\web\Response::FORMAT_JSONP|JSONP]]:通过 [[yii\web\JsonResponseFormatter]] 来实现。
|
||||||
* [[yii\web\Response::FORMAT_RAW|RAW]]: use this format if you want to send the response directly without applying any formatting.
|
* [[yii\web\Response::FORMAT_RAW|RAW]]:如果要直接发送响应而不应用任何格式,请使用此格式。
|
||||||
|
|
||||||
上述响应主体可明确地被设置,但是在大多数情况下是通过[操作](structure-controllers.md)
|
上述响应主体可明确地被设置,但是在大多数情况下是通过[操作](structure-controllers.md)
|
||||||
方法的返回值隐式地设置,常用场景如下所示:
|
方法的返回值隐式地设置,常用场景如下所示:
|
||||||
@ -187,15 +187,14 @@ public function actionOld()
|
|||||||
*临时* 放在另一个 URI 地址上,可传递一个 301 状态码告知浏览器请求
|
*临时* 放在另一个 URI 地址上,可传递一个 301 状态码告知浏览器请求
|
||||||
的资源已经 *永久* 重定向到新的 URId 地址。
|
的资源已经 *永久* 重定向到新的 URId 地址。
|
||||||
|
|
||||||
如果当前请求为 AJAX 请求,
|
如果当前请求为 AJAX 请求,发送一个 `Location` 头不会自动使浏览器跳转,为解决这个问题,
|
||||||
发送一个 `Location` 头不会自动使浏览器跳转,为解决这个问题,
|
|
||||||
[[yii\web\Response::redirect()]] 方法设置一个值为要跳转的URL的 `X-Redirect` 头,
|
[[yii\web\Response::redirect()]] 方法设置一个值为要跳转的URL的 `X-Redirect` 头,
|
||||||
在客户端可编写 JavaScript
|
在客户端可编写 JavaScript
|
||||||
代码读取该头部值然后让浏览器跳转对应的 URL。
|
代码读取该头部值然后让浏览器跳转对应的 URL。
|
||||||
|
|
||||||
> Info: Yii 配备了一个 `yii.js` JavaScript 文件提供常用 JavaScript 功能,
|
> Info: Yii 配备了一个 `yii.js` JavaScript 文件提供常用 JavaScript 功能,
|
||||||
包括基于 `X-Redirect` 头的浏览器跳转,
|
包括基于 `X-Redirect` 头的浏览器跳转,
|
||||||
因此,如果你使用该 JavaScript 文件(通过 [[yii\web\YiiAsset]] 资源包注册),
|
因此,如果你使用该 JavaScript 文件(通过 [[yii\web\YiiAsset]] 资源包注册),
|
||||||
就不需要编写 AJAX 跳转的代码。
|
就不需要编写 AJAX 跳转的代码。
|
||||||
|
|
||||||
## 发送文件 <span id="sending-files"></span>
|
## 发送文件 <span id="sending-files"></span>
|
||||||
@ -259,4 +258,3 @@ Web 应用可在服务器发送文件前结束,为使用该功能,
|
|||||||
|
|
||||||
如你所见 [[yii\web\Response::send()]] 触发了几个实用的事件,
|
如你所见 [[yii\web\Response::send()]] 触发了几个实用的事件,
|
||||||
通过响应这些事件可调整或包装响应。
|
通过响应这些事件可调整或包装响应。
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user