appendTimestamp and registerAssetBundle

2019-07-26 21:36发布

I am using the following configurations to add the timestamp in the assets path via asset manager.

'assetManager' => [
      'appendTimestamp' => true,

appAsset.php

class AppAsset extends AssetBundle
{   
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        '/css/style.css'
    ];
}

In view

AppAsset::register($this);

But in html I still see '/css/style.css' without timestamp.

I saw solution https://github.com/yiisoft/yii2/issues/9101#issuecomment-267274327

$file = '/css/style.css';
$asset = new \yii\web\AssetBundle(
    [
        'js' => [ltrim($file, '/')],
        'basePath' => '@webroot',
        'baseUrl' => '/'
    ]
);
$this->getAssetManager()->bundles[$file] = $asset;
$this->registerAssetBundle($file);

This example is work. '/css/style.css?v=12557565'

Is there any method write registerAssetBundle() not in a view files?

2条回答
叛逆
2楼-- · 2019-07-26 22:08

First of all, what is public $sourcePath = '@frontend/assets';? it is not the correct path this path actually has your Asset.php files, not the source files. According to the documentation

sourcePath: specifies the root directory that contains the asset files in this bundle. This property should be set if the root directory is not Web accessible. Otherwise, you should set the basePath property and baseUrl, instead. Path aliases can be used here.

Just place your .css files under frontend/web/css and .js under frontend/web/js and remove the sourcePath you do not need it as looking at your code, only in case if you had your source files under frontend/themes/basic then you would use sourcePath like public $sourcePath = '@frontend/themes/basic/'; because this path isnt under the web accessable directory

And for adding the timestamp You do not have to do anything other than adding the appendTimestamp options under the components section in the assetManager, make sure you are adding assetManager under components. and it will automatically start adding ?v=125453612 with all CSS and js files

your AppAsset.php

class AppAsset extends AssetBundle
{   
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/style.css'
    ];
}

frontend/config/main.php

'components' => [
..........
..........
    'assetManager' => [
        'appendTimestamp' => true
    ]
..........
..........
],

and your file will start looking like this

enter image description here

I can show you a live example of the same site if you want to have a look into the view-source


EDIT

As you provided the source code screen grab and looking at your code I can tell that you are using trailing slash / with your files, remove the trailing slashes from your source files path, I also overlooked it the first glance but then looking the code i couldn't find any other thing than this one that could be wrong. they should be like this

'css/new.css',
'css/new2.css',

and same for the js files too.

查看更多
beautiful°
3楼-- · 2019-07-26 22:29

Try write public $css and public $js without forst slash ("js/.." instead "/js/..").

查看更多
登录 后发表回答