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?
First of all, what is
public $sourcePath = '@frontend/assets';
? it is not the correct path this path actually has yourAsset.php
files, not the source files. According to the documentationJust place your
.css
files underfrontend/web/css
and.js
underfrontend/web/js
and remove thesourcePath
you do not need it as looking at your code, only in case if you had your source files underfrontend/themes/basic
then you would usesourcePath
likepublic $sourcePath = '@frontend/themes/basic/';
because this path isnt under theweb
accessable directoryAnd for adding the timestamp You do not have to do anything other than adding the
appendTimestamp
options under thecomponents
section in theassetManager
, make sure you are addingassetManager
under components. and it will automatically start adding?v=125453612
with allCSS
andjs
filesyour
AppAsset.php
frontend/config/main.php
and your file will start looking like this
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 thisand same for the
js
files too.Try write
public $css
andpublic $js
without forst slash ("js/.." instead "/js/..").