Search code examples
phpyii2response

How to response errors only in json format in Yii2?


As usual, Yii2 shows errors in HTML format in site/error file. How to make all errors returned as JSON format?


Solution

  • You could change the format of your response in a certain action of your controller, for example:

    public function actionYourAction($id_survey) {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        // return JSON Array or JSON Object here.
    }
    

    But in your case, you must first overwrite the error action, as explained here: https://www.yiiframework.com/doc/api/2.0/yii-web-erroraction