Asset for multiple views in Yii2

66 Views Asked by At

So i am into the challenge of in passing the asset for a number of different views.

As far as I am concerned, there are some ways, apart from adding

use app\assets\SomeAsset;
SomeAsset::register($this);

to the layout page or calling out this method.

Although, I am interested, are there any more flexible ways of passing an asset or a group of assets to the whole crud model at once, for example.

1

There are 1 best solutions below

0
On

Wiggling around me and my colleague found out that this way works as I wanted to:

use app\assets\SomeAsset;
//...Some other dependencies

class DefaultController 
{
    // add this at the beginning of class in the controller
    public function beforeAction($action)
    {
        SomeAsset::register($this->view);
        return parent::beforeAction($action);
    }
}

So this might help if you want to pass asset to all files that are under DefaultController control.

Hope this will be useful to somebody.