R
R
Ruslan Absalyamov2019-04-26 10:56:59
Yii
Ruslan Absalyamov, 2019-04-26 10:56:59

How to reconnect the error page depending on the error status?

My problem is that I can't reconnect the error page. I have a 404 page and 500. What did I do and what happened. According to the documentation, I looked at the frontend2/config/main.php configs. It stands there

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

In the controller
namespace frontend2\controllers;
use yii\web\Controller;

class SiteController extends FrontendController
...
 /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }
/**
     * @brief Страница ошибки
     * @return string
     */
    public function actionError()
    {
        $exception = Yii::$app->errorHandler->exception;
        if ($exception !== null) {
            if ($exception->statusCode == 404) {
                return $this->render('404', ['exception' => $exception]);
            } else if ($exception->statusCode == 404) {
                return $this->render('500', ['exception' => $exception]);
            } else {
                return $this->render('error', ['exception' => $exception]);
            }
        }
    }
}

But all the same, what is indicated by error.php comes out. At the same time, it does not even go into actionError, I tried to dovar_dump('Hello');die()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2019-04-26
@rusline18

Good afternoon.
In the configuration, specify the path to your view for error output. In the view itself, check what the error status is and display the content accordingly.

// в конфигурационном файле
        'errorHandler' => [
            'errorAction' => 'path/to/error/view',
        ],

// в Вашем файле вывода ошибок
if($exception->statusCode == 404){
  $this->title => 'Текст заголовка';
  // остальные переменные
}
if($exception->statusCode == 500){
  $this->title => 'Текст заголовка';
  // остальные переменные
}
// остальной код файла

M
Moses Fender, 2019-04-26
@mosesfender

Well, yii\web\ErrorAction is also indicated to him.
Make your own frontend\components\ErrorAction and specify it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question