http访问get链接得到一个xml的结果,而postman访问的get链接得到的是json的结果,怎么保持一致,
方法一.在输出之前,强制定义下输出格式
<?php
namespace api\controllers;
use yii\rest\Controller;
use api\models\Article;
use yii\db\Query;
//自定义资源
class Top3Controller extends Controller
{
public function actionIndex()
{
// \Yii::$app->response->format = \yii\web\Response::FORMAT_XML;
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$top3 = (new Query())->from('article')->select(['*'])->orderBy('id desc')->limit(3)->all();
return $top3;
}
}
?>
方法二
api\config\main.php文件的components中增加对response的配置
'response' => [
'class' => 'yii\web\Response',
'on beforeSend' => function ($event) {
$response = $event->sender;
$response->format = yii\web\Response::FORMAT_JSON;
},
],