Problem with AssetBundle depend position

Hi,

In my application, I want to do some extra js work, so I extended the AppAsset Class like this:




class AppAsset extends AssetBundle

{

    public $basePath = '@webroot';

    public $baseUrl = '@web';

    public $css = [

        'css/site.css',

    ];

    public $js = [

        'js/global.js'

    ];

    public $depends = [

        'yii\web\YiiAsset',

        'yii\bootstrap\BootstrapAsset',

    ];

}



  1. With default layout

use app\assets\AppAsset;


/* @var $this \yii\web\View */

/* @var $content string */


AppAsset::register($this);

?>

<?php $this->beginPage() ?>

the javascripts load like this when view source page:


<script src="/dev/app/web/assets/46428b7b/jquery.js"></script>

<script src="/dev/app/web/assets/bdb967c7/yii.js"></script>

<script src="/dev/app/web/global.js"></script>

<script src="/dev/app/web/assets/bf24b85e/js/bootstrap.js"></script>

All the bootstrap js in global.js not working, because the page register bootstrap.js after global.js

  1. I move the AppAsset register to bottom of content like this:



//...

</footer>

<?php 

AppAsset::register($this);

$this->endBody(); ?>



then the javascripts load:




<script src="/dev/app/web/assets/46428b7b/jquery.js"></script>

<script src="/dev/app/web/assets/bf24b85e/js/bootstrap.js"></script>

<script src="/dev/app/web/assets/bdb967c7/yii.js"></script>

<script src="/dev/app/web/global.js"></script>

And all the functions of bootstrap js in global.js work fine.

But now I need register many AssetBundle, base on separate view. Because I don’t want to create many layout, so I remove AppAsset register in views/layouts/main.php, then register AppAsset in bottom of a view, i.e: views/site/index.php

After I did that, the script load like case 1:


<script src="/dev/app/web/assets/46428b7b/jquery.js"></script>

<script src="/dev/app/web/assets/bdb967c7/yii.js"></script>

<script src="/dev/app/web/global.js"></script>

<script src="/dev/app/web/assets/bf24b85e/js/bootstrap.js"></script>

What is wrong? How can I register an AssetBundle in a view? Or is there another way to use separate AssetBundle with the same layout?

Please help.

Weird, dependencies should be applied before the requested files. Have you removed the content of ‘web/assets’ folder after making the change?

Hi!

Regading to what Bizley said:

IF the web/assets folder is the reason and you don’t want to delete the assets folder on every change you made… you can do following in app/config/web.php:




if (YII_ENV_DEV) {

    // configuration adjustments for 'dev' environment

    $config['bootstrap'][] = 'debug';

    $config['modules']['debug'] = 'yii\debug\Module';


    $config['bootstrap'][] = 'gii';

    $config['modules']['gii'] = 'yii\gii\Module';


    // ########################################################## ADD THIS

    // force asset copy on every page load 

    $config['components']['assetManager']['forceCopy'] = true;

}



Regards

Hi, thank for your reply.

I deleted all the folders in web\assets. I also add the config.forceCopy like MetaCrawler, but nothing change. When I register AppAsset in index.php view file, the bootstrap.js always include after my global.js file :(

I use yii2 basic. Could you try to reproduce this issue on your computers? I attached my code files too. Please check them.

If it’s an issue, I will report it to the Core team.

Thank in advance.

The Bootstrap extension contains multiple asset bundle classes. If your javascript code depends on bootstrap.js you have to add BootstrapPluginAsset as a dependency.




 public $depends = [

  'yii\web\YiiAsset',

  'yii\bootstrap\BootstrapAsset',

  'yii\bootstrap\BootstrapPluginAsset',

];



It works like a charm! Thank you very much!