yii2响应(Responses)
状态码
Yii::$app->response->statusCode = 200;
异常
yii\web\BadRequestHttpException: status code 400.
yii\web\ConflictHttpException: status code 409.
yii\web\ForbiddenHttpException: status code 403.
yii\web\GoneHttpException: status code 410.
yii\web\MethodNotAllowedHttpException: status code 405.
yii\web\NotAcceptableHttpException: status code 406.
yii\web\NotFoundHttpException: status code 404.
yii\web\ServerErrorHttpException: status code 500.
yii\web\TooManyRequestsHttpException: status code 429.
yii\web\UnauthorizedHttpException: status code 401.
yii\web\UnsupportedMediaTypeHttpException: status code 415.
HTTP头
headers = Yii::$app->response->headers;
// 增加一个 Pragma 头,已存在的Pragma 头不会被覆盖。
$headers->add('Pragma', 'no-cache');
// 设置一个Pragma 头. 任何已存在的Pragma 头都会被丢弃
$headers->set('Pragma', 'no-cache');
// 删除Pragma 头并返回删除的Pragma 头的值到数组
$values = $headers->remove('Pragma');
响应主体
Yii::$app->response->content = 'hello world!'
$response = Yii::$app->response;
$response->format = \yii\web\Response::FORMAT_JSON;
$response->data = ['message' => 'hello world'];
发送文件
yii\web\Response::sendFile(): 发送一个已存在的文件到客户端
yii\web\Response::sendContentAsFile(): 发送一个文本字符串作为文件到客户端
yii\web\Response::sendStreamAsFile(): 发送一个已存在的文件流作为文件到客户端
public function actionDownload()
{
return \Yii::$app->response->sendFile('path/to/file.txt');
}
标签:status,web,code,Responses,yii,响应,Pragma,yii2,response
From: https://www.cnblogs.com/hu308830232/p/18105436