mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 13:58:55 +08:00
@ -120,7 +120,7 @@ public function actionIndex()
|
||||
上述的 `index` 操作返回 `index` 视图渲染结果,
|
||||
返回值会被 `response` 组件格式化后发送给终端用户。
|
||||
|
||||
因为响应格式默认为[[yii\web\Response::FORMAT_HTML|HTML]], 只需要在操作方法中返回一个字符串,
|
||||
因为响应格式默认为 [[yii\web\Response::FORMAT_HTML|HTML]],只需要在操作方法中返回一个字符串,
|
||||
如果想使用其他响应格式,应在返回数据前先设置格式,
|
||||
例如:
|
||||
|
||||
@ -203,9 +203,9 @@ public function actionOld()
|
||||
和浏览器跳转类似,文件发送是另一个依赖指定 HTTP 头的功能,
|
||||
Yii 提供方法集合来支持各种文件发送需求,它们对 HTTP 头都有内置的支持。
|
||||
|
||||
- [[yii\web\Response::sendFile()]]: 发送一个已存在的文件到客户端
|
||||
- [[yii\web\Response::sendContentAsFile()]]: 发送一个文本字符串作为文件到客户端
|
||||
- [[yii\web\Response::sendStreamAsFile()]]: 发送一个已存在的文件流作为文件到客户端
|
||||
- [[yii\web\Response::sendFile()]]:发送一个已存在的文件到客户端
|
||||
- [[yii\web\Response::sendContentAsFile()]]:发送一个文本字符串作为文件到客户端
|
||||
- [[yii\web\Response::sendStreamAsFile()]]:发送一个已存在的文件流作为文件到客户端
|
||||
|
||||
这些方法都将响应对象作为返回值,如果要发送的文件非常大,应考虑使用
|
||||
[[yii\web\Response::sendStreamAsFile()]] 因为它更节约内存,
|
||||
@ -246,13 +246,13 @@ Web应用可在服务器发送文件前结束,为使用该功能,
|
||||
|
||||
[[yii\web\Response::send()]] 方法使用以下步骤来发送响应:
|
||||
|
||||
1. 触发 [[yii\web\Response::EVENT_BEFORE_SEND]] 事件.
|
||||
1. 触发 [[yii\web\Response::EVENT_BEFORE_SEND]] 事件。
|
||||
2. 调用 [[yii\web\Response::prepare()]] 来格式化 [[yii\web\Response::data|response data]] 为
|
||||
[[yii\web\Response::content|response content]].
|
||||
3. 触发 [[yii\web\Response::EVENT_AFTER_PREPARE]] 事件.
|
||||
[[yii\web\Response::content|response content]]。
|
||||
3. 触发 [[yii\web\Response::EVENT_AFTER_PREPARE]] 事件。
|
||||
4. 调用 [[yii\web\Response::sendHeaders()]] 来发送注册的HTTP头
|
||||
5. 调用 [[yii\web\Response::sendContent()]] 来发送响应主体内容
|
||||
6. 触发 [[yii\web\Response::EVENT_AFTER_SEND]] 事件.
|
||||
6. 触发 [[yii\web\Response::EVENT_AFTER_SEND]] 事件。
|
||||
|
||||
一旦 [[yii\web\Response::send()]] 方法被执行后,其他地方调用该方法会被忽略,
|
||||
这意味着一旦响应发出后,就不能再追加其他内容。
|
||||
|
||||
Reference in New Issue
Block a user