xoons
xoons

Reputation: 31

Yii2 How to enable default assets only in certain modules

I am using basic yii2 app, I have disabled the boostrap css and js in web.php config but i want to enable it inside the module called admin.

I tried adding like this, for component section in config.php under admin module folder, but no luck!

'assetManager' => [
       'bundles' => [
           'yii\bootstrap\BootstrapPluginAsset' => [
                'js'=> ['js/boostrap.js']
            ],
           'yii\bootstrap\BootstrapAsset' => [
               'css' => ['css/boostrap.min.css']
           ]
        ],
    ],

How can i achieve this?

Upvotes: 3

Views: 4180

Answers (2)

soju
soju

Reputation: 25302

You should use asset bundles to handle this, no need to modify assetManager.

To disable Bootstrap, simply remove it from $depends in AppAsset :

public $depends = [
    'yii\web\YiiAsset',
];

Create a ModuleAsset class in your module :

class ModuleAsset extends \yii\web\AssetBundle
{
    public $depends = [
        'yii\bootstrap\BootstrapAsset',
    ];
}

Make sure to register it in your module's views (you could use a custom layout) :

ModuleAsset::register($this);

Read more : http://www.yiiframework.com/doc-2.0/guide-structure-assets.html

Upvotes: 4

Ali MasudianPour
Ali MasudianPour

Reputation: 14459

In your module's init() method, you can override an exist asset bundle like below:

public function init() {
    \Yii::$app->assetManager->bundles['yii\bootstrap\BootstrapAsset'] = [
        'css' => ['css/bootstrap_NEW.css']
    ];
    parent::init();
}

In above example, we override yii\bootstrap\BootstrapAsset CSS file in our module. So, in this module, it uses bootstrap_NEW.css instead of its default.

Please note that, you can do this in application globally, not specially in module's init() method. It was just a suggestion to solve this specific case.

Upvotes: 5

Related Questions