这篇文章主要介绍了Yii核心组件AssetManager原理分析,较为详细的分析了AssetManager组件的原理与实现过程,有助于深入了解yii框架的特性,需要的朋友可以参考下
本文我们通过yii自带的demo-blog程序来分析Yii核心组件AssetManager,他可以自动加载css和javascript,并且只需要一句代码即可。具体分析如下:
打开blog的首页,会看到如下的引入js的html代码:
复制代码 代码如下:
这些js文件的路径都在assets文件夹下,assets后面跟着一个显然经过hash的文件夹路径,同属于jq的js代码的路径相同,这段代码从何而来呢?
直接看view文件看不到任何引入js的代码,因此应该是使用widget引入的:
复制代码 代码如下:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'template'=>"{items}n{pager}",
));
?>
这个widget也是yii自带的zii扩展,于是乎我们可以找到zii的CListView代码,而CListView又是继承CBaseListView,因此先看CBaseListView的run方法:
复制代码 代码如下:
public function run()
{
$this->registerClientScript();
echo CHtml::openTag($this->tagName,$this->htmlOptions)."n";
$this->renderKeys();
$this->renderContent();
echo CHtml::closeTag($this->tagName);
}
请注意第一个方法registerClientScript,这个方法是在CListView中实现的:
复制代码 代码如下:
public function registerClientScript()
{
……
$cs=Yii::app()->getClientScript();
$cs->registerCoreScript('jquery');
$cs->registerCoreScript('bbq');
……
}
看到jquery和bbp似乎离真相近了些,接下来我们看CClientScript::registerCoreScript方法:
复制代码 代码如下:
public function registerCoreScript($name)
{
$this->_hasScripts=true;
$this->_coreScripts[$name]=$name;
$params=func_get_args();
$this->recordCachingAction('clientScript','registerCoreScript',$params);
}
这里其实主要是记录了最终页面要render的js,而实际生成render的url是通过getCoreScriptUrl方法:
复制代码 代码如下:
public function getCoreScriptUrl()
{
if($this->_baseUrl!==null)
return $this->_baseUrl;
else
return $this->_baseUrl=Yii::app()->getAssetManager()->publish(YII_PATH.'/web/js/source');
}
接下来我们看看publish的具体过程:
复制代码 代码如下:
public function publish($path,$hashByName=false,$level=-1,$forceCopy=false)
{
if(is_file($src))
{
$dir=$this->hash($hashByName ? basename($src) : dirname($src));
$fileName=basename($src);
……
else if(is_dir($src))
{
$dir=$this->hash($hashByName ? basename($src) : $src);
$dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir;
……
}
这里通过对路径做了hash处理,因此我们看到的路径是不规则的,而由于jq系列的js代码均在同一路径下(都在framework/web/js/source下),所以hash值是相同的。
另外,除了如上所述,CAssetManager使得多个模块可以复用相同的代码制外,,使用CAssetManager的另外一个好处是安全隔离,将真实的代码放在受保护的路径下,按需加载。
希望本文所述对大家基于yii框架的PHP程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
