1.HTTP 头部
可在 response 组件中操控header collection来发送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');
2.响应主体
大多是响应应有一个主体存放你想要显示给终端用户的内容。
如果已有格式化好的主体字符串,可赋值到响应的yii\web\Response::$content属性
Yii::$app->response->content = 'hello world!';
如果在发送给终端用户之前需要格式化,应设置 format 和 data 属性,format 属性指定data中数据格式化后的样式,例如:
$response = Yii::$app->response;
$response->format = \yii\web\Response::FORMAT_JSON;
$response->data = ['message' => 'hello world'];
3.把数组返回json数据的方式
public function actionIndex16() {
// throw new \yii\web\ServerErrorHttpException;
// echo json_encode();
// Yii::$app->response->format = 'json';
// \yii\web\Response::$format = 'json';
$response = new \yii\web\Response();
$response->format = 'json';
return [
'msg' => '成功',
'status' => 'ok'
];
}