注意事项:
$this->beginPage(),endBody(),$this->head()等一定要加上
views\layouts\main.php
AppAsset::register($this);
追溯register()
yii\]web\assetBundle.php
/**
* Registers this asset bundle with a view.
* @param View $view the view to be registered with
* @return static the registered asset bundle instance
*/
public static function register($view)
{
return $view->registerAssetBundle(get_called_class());
}
追溯registerAssetBundle()
yii\web\view.php
public function registerAssetBundle($name, $position = null)
{
if (!isset($this->assetBundles[$name])) {
$am = $this->getAssetManager();
$bundle = $am->getBundle($name);
$this->assetBundles[$name] = false;
// register dependencies
$pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null;
foreach ($bundle->depends as $dep) {
$this->registerAssetBundle($dep, $pos);
}
$this->assetBundles[$name] = $bundle;
} elseif ($this->assetBundles[$name] === false) {
throw new InvalidConfigException("A circular dependency is detected for bundle '$name'.");
} else {
$bundle = $this->assetBundles[$name];
}
$am->getBundle($name);
$this->assetBundles[$name] = false;
// register dependencies
$pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null;
foreach ($bundle->depends as $dep) {
$this->registerAssetBundle($dep, $pos);
}
$this->assetBundles[$name] = $bundle;
} elseif ($this->assetBundles[$name] === false) {
throw new InvalidConfigException("A circular dependency is detected for bundle '$name'.");
} else {
$bundle = $this->assetBundles[$name];
}
追溯getBundle()
yii\web\assetManage.php
public function getBundle($name, $publish = true)
{
if ($this->bundles === false) {
return $this->loadDummyBundle($name);
} elseif (!isset($this->bundles[$name])) {
return $this->bundles[$name] = $this->loadBundle($name, [], $publish);
} elseif ($this->bundles[$name] instanceof AssetBundle) {
return $this->bundles[$name];
} elseif (is_array($this->bundles[$name])) {
return $this->bundles[$name] = $this->loadBundle($name, $this->bundles[$name], $publish);
} elseif ($this->bundles[$name] === false) {
return $this->loadDummyBundle($name);
}
throw new InvalidConfigException("Invalid asset bundle configuration: $name");
}
$this->loadBundle($name, [], $publish);
} elseif ($this->bundles[$name] instanceof AssetBundle) {
return $this->bundles[$name];
} elseif (is_array($this->bundles[$name])) {
return $this->bundles[$name] = $this->loadBundle($name, $this->bundles[$name], $publish);
} elseif ($this->bundles[$name] === false) {
return $this->loadDummyBundle($name);
}
throw new InvalidConfigException("Invalid asset bundle configuration: $name");
}
追溯loadBundle()
yii\web\assetManage.php
/**
* Loads asset bundle class by name.
*
* @param string $name bundle name
* @param array $config bundle object configuration
* @param bool $publish if bundle should be published
* @return AssetBundle
* @throws InvalidConfigException if configuration isn't valid
*/
protected function loadBundle($name, $config = [], $publish = true)
{
if (!isset($config['class'])) {
$config['class'] = $name;
}
/* @var $bundle AssetBundle */
$bundle = Yii::createObject($config);
if ($publish) {
$bundle->publish($this);
}
return $bundle;
}
bundle->publish($this);
}
return $bundle;
}
追溯publish()
yii\]web\assetBundle.php
/**
* 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);
}
}
}
}
$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);
}
}
}
}
追溯publish()
yii\]web\assetManger.php
public function publish($path, $options = [])
{
$path = Yii::getAlias($path);
if (isset($this->_published[$path])) {
return $this->_published[$path];
}
if (!is_string($path) || ($src = realpath($path)) === false) {
throw new InvalidParamException("The file or directory to be published does not exist: $path");
}
if (is_file($src)) {
return $this->_published[$path] = $this->publishFile($src);
}
return $this->_published[$path] = $this->publishDirectory($src, $options);
}
/**
* Publishes a file.
* @param string $src the asset file to be published
* @return string[] the path and the URL that the asset is published as.
* @throws InvalidParamException if the asset to be published does not exist.
*/
protected function publishFile($src)
{
$dir = $this->hash($src);
$fileName = basename($src);
$dstDir = $this->basePath . DIRECTORY_SEPARATOR . $dir;
$dstFile = $dstDir . DIRECTORY_SEPARATOR . $fileName;
if (!is_dir($dstDir)) {
FileHelper::createDirectory($dstDir, $this->dirMode, true);
}
if ($this->linkAssets) {
if (!is_file($dstFile)) {
try { // fix #6226 symlinking multi threaded
symlink($src, $dstFile);
} catch (\Exception $e) {
if (!is_file($dstFile)) {
throw $e;
}
}
}
} elseif (@filemtime($dstFile) < @filemtime($src)) {
copy($src, $dstFile);
if ($this->fileMode !== null) {
@chmod($dstFile, $this->fileMode);
}
}
return [$dstFile, $this->baseUrl . "/$dir/$fileName"];
}
$this->publishFile($src);
}
return $this->_published[$path] = $this->publishDirectory($src, $options);
}
/**
* Publishes a file.
* @param string $src the asset file to be published
* @return string[] the path and the URL that the asset is published as.
* @throws InvalidParamException if the asset to be published does not exist.
*/
protected function publishFile($src)
{
$dir = $this->hash($src);
$fileName = basename($src);
$dstDir = $this->basePath . DIRECTORY_SEPARATOR . $dir;
$dstFile = $dstDir . DIRECTORY_SEPARATOR . $fileName;
if (!is_dir($dstDir)) {
FileHelper::createDirectory($dstDir, $this->dirMode, true);
}
if ($this->linkAssets) {
if (!is_file($dstFile)) {
try { // fix #6226 symlinking multi threaded
symlink($src, $dstFile);
} catch (\Exception $e) {
if (!is_file($dstFile)) {
throw $e;
}
}
}
} elseif (@filemtime($dstFile) < @filemtime($src)) {
copy($src, $dstFile);
if ($this->fileMode !== null) {
@chmod($dstFile, $this->fileMode);
}
}
return [$dstFile, $this->baseUrl . "/$dir/$fileName"];
}
这里的basePath,在yii\web\assetManager.php定义的
public $basePath = '@webroot/assets';
/**
* @var string the base URL through which the published asset files can be accessed.
*/
public $baseUrl = '@web/assets';
public $basePath = '@webroot/assets';
/**
* @var string the base URL through which the published asset files can be accessed.
*/
public $baseUrl = '@web/assets';