October 05 2013

Yii different controller return different error page

Tagged Under :

Yii
Because need develop an APi application with frontend and backend together.

Frontend without any interface and it just return JSON format to the user. But backend have interface to let user do the setting and update data.

In Yii main.php only allow set one error page. The solution is set the error page in controller class.

You can follow below example to write your own error page to each controller.

Controller error handler:
class ApiController extends Controller {
    public function init() {        
        parent::init();
        Yii::app()->errorHandler->errorAction = 'api/error';
    }
    public function actionError(){
        echo json_encode(array(
            'Status' => 500,
            'Message' => 'Unknown error'
        ));
    }
}

Make a Comment

You must be logged in to post a comment.