I’ve given my Yii2 application a static end with path, ‘/static’, & url, ‘//static.example.com’, using the following lines in ‘/common/congig/bootstrap.php’…
Yii::setAlias('static', dirname(dirname(__DIR__)) . '/static');
Yii::setAlias('staticUrl', '//static.example.com');
Then for each application end I have at ‘/endname/config/main.php’…
'components' => [
'assetManager' => [
'basePath' => '@static/endname',
'baseUrl' => '@staticUrl/endname',
],
...
],
With that set, an asset…
public $sourcePath = '@static/path/to/asset/lib';
public $css = [
'css/asset.css',
];
public $js = [
'js/asset.js'
];
is published nicely to…
<link href="//static.example.com/endname/34a1a0d3/css/asset.css" rel="stylesheet">
...
<script src="//static.example.com/endname/34a1a0d3/js/asset.js"></script>
& another asset…
public $baseUrl = '@staticUrl/path/to/resource';
public $css = [
'css/asset.css',
];
public $js = [
'js/asset.js'
];
where I put the files there myself, Yii inserts…
<link href="//static.example.com/path/to/resource/css/asset.css" rel="stylesheet">
...
<script src="//static.example.com/path/to/resource/js/asset.js"></script>
That’s 2 out of 3.
However I’d like publish an asset to its own directory, ‘/static/myasset/34a1a0d3’ & ‘//static.example.com/myasset/34a1a0d3’, for use by both application ends. The directory name ‘myasset’ could sensibly be ‘common’, but I’d like to have the asset define it. The desired HTML would be…
<link href="//static.example.com/myasset/34a1a0d3/css/asset.css" rel="stylesheet">
...
<script src="//static.example.com/myasset/34a1a0d3/js/asset.js"></script>
I first thought I could do this by adding $basePath to the asset definition…
public $sourcePath = '@static/path/to/asset/lib';
public $basePath = '@static/myasset';
public $css = [
'css/asset.css',
];
public $js = [
'js/asset.js'
];
But $basePath defined in the asset is ignored for the one defined in config & my asset is published exactly the same as the first example with the same HTML.
Even though it doesn’t reall make sense not to have it, I try removing $sourcePath & have no asset published & HTML…
<link href="/css/asset.css" rel="stylesheet">
...
<script src="/js/asset.js"></script>
If I add…
public $sourcePath = '@static/path/to/asset/lib';
to the asset definition, the ‘$basePath’ defined in the asset is ignored & it’s
If I include $baseUrl in the asset definition…
public $sourcePath = '@static/path/to/asset/lib';
public $basePath = '@static/myasset';
public $baseUrl = '@staticUrl/myasset';
...
I get no asset published & HTML…
<link href="//static.example.com/myasset/css/asset.css" rel="stylesheet">
...
<script src="//static.example.com/myasset/js/asset.js"></script>
Any ideas as to how I can have the assets publish to a directory defined in the asset? Put another way, I’d like to be able to define an asset so that it publishes with ‘endname’ in $basePath & $baseUrl of the first example, swapped for ‘myasset’.