Browse Source

Added ability to use aliases and domain relative URLs at `yii\web\AssetManager::assetMap`

tags/2.0.6
Klimov Paul 9 years ago
parent
commit
95ba06c01b
  1. 7
      framework/CHANGELOG.md
  2. 25
      framework/web/AssetManager.php

7
framework/CHANGELOG.md

@ -20,9 +20,10 @@ Yii Framework 2 Change Log
- Enh #8070: `yii\console\controllers\MessageController` now sorts created messages, even if there is no new one, while saving to PHP file (klimov-paul)
- Enh #8286: `yii\console\controllers\MessageController` improved allowing extraction of nested translator calls (klimov-paul)
- Enh #8415: `yii\helpers\Html` allows correct rendering of conditional comments containing `!IE` (salaros, klimov-paul)
- Enh #8444: Added `yii\widgets\LinkPager::$linkOptions` to allow configuring HTML attributes of the `a` tags (zinzinday)
- Enh #8486: Added support to automatically set the `maxlength` attribute for `Html::activeTextArea()` and `Html::activePassword()` (klimov-paul)
- Enh #8566: Added support for 'only' and 'except' options for `yii\web\AssetManager::publish()` (klimov-paul)
- Enh #8444: Added `yii\widgets\LinkPager::$linkOptions` to allow configuring HTML attributes of the `a` tags (zinzinday)
- Enh #8486: Added support to automatically set the `maxlength` attribute for `Html::activeTextArea()` and `Html::activePassword()` (klimov-paul)
- Enh #8522: Added ability to use aliases and domain relative URLs at `yii\web\AssetManager::assetMap` (klimov-paul)
- Enh #8566: Added support for 'only' and 'except' options for `yii\web\AssetManager::publish()` (klimov-paul)
- Chg #6354: `ErrorHandler::logException()` will now log the whole exception object instead of only its string representation (cebe)

25
framework/web/AssetManager.php

@ -81,7 +81,8 @@ class AssetManager extends Component
* the corresponding value will replace the asset and be registered with the view.
* For example, an asset file `my/path/to/jquery.js` matches a key `jquery.js`.
*
* Note that the target asset files should be either absolute URLs or paths relative to [[baseUrl]] and [[basePath]].
* Note that the target asset files should be absolute URLs, domain relative URLs (starting from '/') or paths
* relative to [[baseUrl]] and [[basePath]].
*
* In the following example, any assets ending with `jquery.min.js` will be replaced with `jquery/dist/jquery.js`
* which is relative to [[baseUrl]] and [[basePath]].
@ -91,6 +92,14 @@ class AssetManager extends Component
* 'jquery.min.js' => 'jquery/dist/jquery.js',
* ]
* ```
*
* You may also use aliases while specifying map value, for example:
*
* ```php
* [
* 'jquery.min.js' => '@web/js/jquery/jquery.js',
* ]
* ```
*/
public $assetMap = [];
/**
@ -261,15 +270,21 @@ class AssetManager extends Component
public function getAssetUrl($bundle, $asset)
{
if (($actualAsset = $this->resolveAsset($bundle, $asset)) !== false) {
$asset = $actualAsset;
$basePath = $this->basePath;
$baseUrl = $this->baseUrl;
if (strncmp($actualAsset, '@web/', 5) === 0) {
$asset = substr($actualAsset, 5);
$basePath = Yii::getAlias("@webroot");
$baseUrl = Yii::getAlias("@web");
} else {
$asset = Yii::getAlias($actualAsset);
$basePath = $this->basePath;
$baseUrl = $this->baseUrl;
}
} else {
$basePath = $bundle->basePath;
$baseUrl = $bundle->baseUrl;
}
if (!Url::isRelative($asset)) {
if (!Url::isRelative($asset) || strncmp($asset, '/', 1) === 0) {
return $asset;
}

Loading…
Cancel
Save