yii设置返回数据为JSON格式

<?php 
namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use  yii\web\Response;


/**
* 获取店铺列表
*/
class ShopController extends Controller
{
    
    public function actionShop()
    {

        $data = (new \yii\db\Query())
                /*->select(['id','email'])*/
                ->from('wd_b_shop')
                ->limit(10)
                ->all();
         Yii::$app->response->format=Response::FORMAT_JSON;//在返回值之前加一句,设置返回格式是JSON
         return ['code'=>false,'message'=>$data];
    }
}
 ?>

完美解决

posted on 2017-03-23 09:02  FantasyBoy  阅读(1757)  评论(0)    收藏  举报