D
D
Dmitry Kim2016-06-24 07:08:42
Yii
Dmitry Kim, 2016-06-24 07:08:42

How to display error page in YII2 controller?

As you know, by default, errors are handled in the error action of the site controller:

'components'          => [
            'errorHandler' => [
                'errorAction' => 'site/error',
            ],
...

And if, for example, I need the error action in the news controller to display the error in a separate news module - i.e. inside news, the error is handled by news/error, and other errors by site/error.
How to do it, and is it possible?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2016-06-24
@kimono

And what prevents you from looking at which controller in the 'site/error' action and calling this or that view or this or that algorithm already there?
or like this

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'
        ));
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question