H
H
Habr2019-02-09 12:40:45
Yii
Habr, 2019-02-09 12:40:45

How to load separate layout if Ajax request?

How can I load a separate layout if the server sends an Ajax request?

public function init()
{
    parent::init();

    if (Yii::$app->request->isAjax){
        $this->layout = '/ajax_page';
    }else{
        $this->layout = '/blank-panel';
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2019-02-11
@GA_Roman

You are apparently trying to do it in a module (judging by init), and there inside $this is a module (which is logical). So what is needed in the controller in beforeAction:

public function beforeAction($action)
{
    $this->layout = Yii::$app->request->isAjax ? '/ajax_page' : '/blank-panel';
    return parent::beforeAction($action);
}

But morricone85 is talking. This is a strange thing. If ajax, then layout is not important under normal conditions.

M
morricone85, 2019-02-09
@morricone85

do you need layout or view? Usually, when using Ajax, the renderAjax() method is used.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question