2

I have a module in Yii2 containing a lot of controllers, models and views.

How can I register an asset for all views, without register it in all view one by one?

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Tibor Nagy
  • 1,185
  • 1
  • 15
  • 28

1 Answers1

3

The module has init() method, you can use it for code that needs to be executed every time the module is accessed:

<?php

namespace frontend\modules\users;

use frontend\assets\UsersAsset;
use Yii;
use yii\base\Module as BaseModule;

class Module extends BaseModule
{
    /**
     * @inheritdoc
     */
    public $controllerNamespace = 'frontend\modules\users\controllers';

    /**
     * @inheritdoc
     */
    public function init()
    {
       UsersAsset::register(Yii::$app->view);       

       parent::init();            
    }
}

Don't forget to call parent implementation.

arogachev
  • 33,150
  • 7
  • 114
  • 117