1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public function beforeAction($action)
{
    if(condition){
        Yii::$app->response->format = Response::FORMAT_JSON;
        Yii::$app->response->data = array(
            'status' => -1,
            'message' => '请先登录',
            'url' => \Yii::$app->getHomeUrl()
        );
        return false;
    }

    return true;
}

这个函数只返回bool值,响应内容交给 response

原文