yii\web\AssetManager::getAssetUrl PHP Метод

getAssetUrl() публичный Метод

The actual URL is obtained by prepending either [[AssetBundle::$baseUrl]] or [[AssetManager::$baseUrl]] to the given asset path.
public getAssetUrl ( AssetBundle $bundle, string $asset ) : string
$bundle AssetBundle the asset bundle which the asset file belongs to
$asset string the asset path. This should be one of the assets listed in [[AssetBundle::$js]] or [[AssetBundle::$css]].
Результат string the actual URL for the specified asset.
    public function getAssetUrl($bundle, $asset)
    {
        if (($actualAsset = $this->resolveAsset($bundle, $asset)) !== false) {
            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) || strncmp($asset, '/', 1) === 0) {
            return $asset;
        }
        if ($this->appendTimestamp && ($timestamp = @filemtime("{$basePath}/{$asset}")) > 0) {
            return "{$baseUrl}/{$asset}?v={$timestamp}";
        } else {
            return "{$baseUrl}/{$asset}";
        }
    }

Usage Example

Пример #1
0
 public function getAssetUrl($bundle, $asset)
 {
     $url = parent::getAssetUrl($bundle, $asset);
     $absPath = \Yii::getAlias('@webroot') . $url;
     if (is_file($absPath)) {
         $mtime = filemtime($absPath);
         $md = md5($mtime);
         $v = substr($md, 0, 8);
         $url .= StringHelper::contains($url, '?') ? '&' : '?';
         $url .= "v={$v}";
     }
     return $url;
 }