Yii2 - app assets

13,398

Solution 1

You should simply try:

class PrologueAssets extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        ...
    ];
    public $js = [
        ...
    ];
}

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

PS : assets management is currently being refactored : https://github.com/yiisoft/yii2/pull/4855

Solution 2

I wanted to add two files,one under frontend/web/css/custom.css and frontend/web/js/custom.js I just found the frontend/assets/AppAsset.php and added them directly and the two files are loaded for every page i load.

class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
        'css/custom.css',
    ];
    public $js = [
        'js/custom.js',
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}
Share:
13,398

Related videos on Youtube

Joe
Author by

Joe

Web, desktop and DB developer. Passionate about databases. Very happy to do anything in C#. Enthusiast of logic, objectivity, patterns and abstraction!

Updated on September 16, 2022

Comments

  • Joe
    Joe over 1 year

    Yii is great but assets is the thing that was always strange for me (since the 1.1 version). I'm using Yii2 currently with Advanced app template. I want to register some css/js files in frontend main layout view (trying to use HTML5UP Prologue Template). How it can be done? I placed my css files under frontend/web/css directory, js under frontend/web/js and layout images under frontend/web/images dir.

    Under frontend/assets directory I created PrologueAssets class like this:

    namespace frontend\assets;
    use yii\web\AssetBundle;
    
    class PrologueAssets extends AssetBundle {
    
        public $sourcePath = "@webroot";
        public $css = [
            'css/skel.css',
            'css/style.css',
            'css/style-wide.css',
        ];
        public $js = [
            'js/jquery.min.js',
            'js/jquery.scrolly.min.js',
            'js/jquery.scrollzer.min.js',
            'js/skel.min.js',
            'js/skel-layers.min.js',
            'js/init.js',
        ];
    
        static function register($view) {
            die(self::sourcePath);
            parent::register($view);
        }
    
    }
    

    Unfortunately none of these files are registered. How to do that?

    Little second question - how to register css files only if eg IE8 or IE9 is detected?

    • Prabowo Murti
      Prabowo Murti
      What happens when you include the CSS and JS files on AppAsset.php (instead of PrologueAssets.php?
    • Joe
      Joe
      I made a workaround - included the files in view using Url::base() to generate base url and by adding css/... or js/... to the end. I always thought assets are to complicated in Yii.
    • Joe
      Joe
      @PrabowoMurti - that was interesting. It tried to include the files but not in relation to base url but the current so it was working only on homepage. Pretty weird, ecpesially when at the same time the default yiis site.css is loaded properly.