A
A
akula222016-10-13 10:26:19
Yii
akula22, 2016-10-13 10:26:19

How to display errors in another template in yii2?

I use the basic template, made the admin panel a module, and in the admin panel (Backend) all errors are displayed in the public template
Tell me how to display all errors in the backend template.
Now all the errors use the /view/layouts/main.php template
and I need /backend/view/layouts/main.php
In all backend controllers I use
public $layout ='@app/modules/backend/views/layouts/main';
here is how it for errors to register?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-10-13
@qonand

The application component ErrorHandler is responsible for handling errors, and the action specified in the errorAction parameter of the component is responsible for rendering the error page itself. Accordingly, when rendering the error page, the layer specified in the controller to which the errorAction refers is used. Therefore, in the simplest solution, you can define a layer at this location.
For example:
Application configuration:

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

Controller
namespace app\controllers;

use Yii;
use yii\web\Controller;

class SiteController extends Controller
{
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

public function beforeAction($action)
{
    if ($action == 'error' && Yii->app->user->isGuest) {
        $this->layout = '@app/modules/backend/views/layouts/main';
    }
    return parent::beforeAction($action);
}
}

but again, this is the easiest option out of many

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question