AssetManager is configured as an application component in Application by default.
You can access that instance via Yii::$app->assetManager.
You can modify its configuration by adding an array to your application config under components
as shown in the following example:
php
'assetManager' => [
'bundles' => [
you can override AssetBundle configs here
],
]
For more details and usage information on AssetManager, see the guide article on assets.
/** * Publishes the asset bundle if its source code is not under Web-accessible directory. * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding * CSS or JS files using [[AssetManager::converter|asset converter]]. * @param \yii\web\AssetManager $am the asset manager to perform the asset publishing */ public function publish($am) { if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) { list($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions); } $converter = $am->getConverter(); foreach ($this->js as $i => $value) { if (is_array($value)) { if (isset($value['file'])) { $js = $value['file']; } else { continue; } } else { $js = $value; } if (strpos($js, '/') !== 0 && strpos($js, '://') === false) { if (isset($this->basePath, $this->baseUrl)) { $js = $converter->convert($js, $this->basePath); } else { $js = '/' . $js; } if (is_array($value)) { $value['file'] = $js; } else { $value = $js; } $this->js[$i] = $value; } } foreach ($this->css as $i => $value) { if (is_array($value)) { if (isset($value['file'])) { $css = $value['file']; } else { continue; } } else { $css = $value; } if (strpos($css, '/') !== 0 && strpos($css, '://') === false) { if (isset($this->basePath, $this->baseUrl)) { $css = $converter->convert($css, $this->basePath); } else { $css = '/' . $css; } if (is_array($value)) { $value['file'] = $css; } else { $value = $css; } $this->css[$i] = $value; } } }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->useCdn) { $this->baseUrl = Yii::$app->cdnManager->getBaseUrl() . '/' . Yii::getAlias('@kalibao/app') . '/' . $this->baseUrl; } else { $this->baseUrl = '/' . $this->baseUrl; } }
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; }
/** * Publishes the asset bundle if its source code is not under Web-accessible directory. * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding * CSS or JS files using [[AssetManager::converter|asset converter]]. * @param AssetManager $am the asset manager to perform the asset publishing */ public function publish($am) { if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) { list($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions); } if (isset($this->basePath, $this->baseUrl) && ($converter = $am->getConverter()) !== null) { foreach ($this->js as $i => $js) { if (is_array($js)) { $file = array_shift($js); if (Url::isRelative($file)) { $js = ArrayHelper::merge($this->jsOptions, $js); array_unshift($js, $converter->convert($file, $this->basePath)); $this->js[$i] = $js; } } elseif (Url::isRelative($js)) { $this->js[$i] = $converter->convert($js, $this->basePath); } } foreach ($this->css as $i => $css) { if (is_array($css)) { $file = array_shift($css); if (Url::isRelative($file)) { $css = ArrayHelper::merge($this->cssOptions, $css); array_unshift($css, $converter->convert($file, $this->basePath)); $this->css[$i] = $css; } } elseif (Url::isRelative($css)) { $this->css[$i] = $converter->convert($css, $this->basePath); } } } }
/** * Publishes the asset bundle if its source code is not under Web-accessible directory. * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding * CSS or JS files using [[AssetManager::converter|asset converter]]. * @param AssetManager $am the asset manager to perform the asset publishing */ public function publish($am) { if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) { list($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions); } if (isset($this->basePath, $this->baseUrl) && ($converter = $am->getConverter()) !== null) { foreach ($this->js as $i => $js) { if (Url::isRelative($js)) { $this->js[$i] = $converter->convert($js, $this->basePath); } } foreach ($this->css as $i => $css) { if (Url::isRelative($css)) { $this->css[$i] = $converter->convert($css, $this->basePath); } } } }
/** * Publishes the asset bundle if its source code is not under Web-accessible directory. * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding * CSS or JS files using [[AssetManager::converter|asset converter]]. * @param AssetManager $am the asset manager to perform the asset publishing */ public function publish($am) { if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) { list($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions); } $converter = $am->getConverter(); foreach ($this->js as $i => $js) { if (strpos($js, '/') !== 0 && strpos($js, '://') === false) { if (isset($this->basePath, $this->baseUrl)) { $this->js[$i] = $converter->convert($js, $this->basePath); } else { $this->js[$i] = '/' . $js; } } } foreach ($this->css as $i => $css) { if (strpos($css, '/') !== 0 && strpos($css, '://') === false) { if (isset($this->basePath, $this->baseUrl)) { $this->css[$i] = $converter->convert($css, $this->basePath); } else { $this->css[$i] = '/' . $css; } } } }
protected function publishFile($src) { throw new \yii\base\Exception('Not implemented!'); //TODO: check custom behavior return parent::publishFile($src); }
public function registerAssetFiles($view) { parent::registerAssetFiles($view); $m = new AssetManager(); $m->publish('@webroot/downloads/images/'); }