Tell me how to make your error page in yii2. Created view error.php in views / site. Registered in the controller

public function actionError() { $this->render('error'); } 

In config / web.php

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

But when you call

 throw new \yii\web\HttpException(404 ,'Страница не найдена'); 

redirection to the error page does not occur, the browser error page is displayed.

    1 answer 1

    In controllers, where you plan to throw out the desired error page, you must have spelled out the actions or behaviors method

     public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], ... // тут еще какие-то опции могут быть ]; } 

    In the config, you all spelled correctly

    In the controller, you do not need the public function actionError() method

    Accordingly, if you want to "artificially" cause an error, then you need to throw new \yii\web\ErrorAction(404 ,'Страница не найдена'); away throw new \yii\web\ErrorAction(404 ,'Страница не найдена'); or throw new yii\web\NotFoundHttpException('Страница не найдена');

    • With ErrorAction error, but with NotFoundHttpException everything is fine. Thank! - t16bz