Home > Backend Development > PHP Tutorial > Yii core component AssetManager principle analysis, yiiassetmanager_PHP tutorial

Yii core component AssetManager principle analysis, yiiassetmanager_PHP tutorial

WBOY
Release: 2016-07-13 10:12:39
Original
918 people have browsed it

Principle analysis of Yii core component AssetManager, yiiassetmanager

In this article, we use the demo-blog program that comes with yii to analyze the core component AssetManager of Yii. It can automatically load css and javascript and only requires one line of code. The specific analysis is as follows:

Open the homepage of the blog and you will see the following html code that introduces js:

Copy code The code is as follows:



The paths of these js files are all in the assets folder. Assets is followed by a folder path that has obviously been hashed. The path of the js code that belongs to jq is the same. Where does this code come from?

Looking directly at the view file, you can’t see any code that introduces js, so it should be introduced using widget:

Copy code The code is as follows:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'template'=>"{items}n{pager}",
));
?>

This widget is also a Zii extension that comes with Yii, so we can find Zii’s CListView code, and CListView inherits CBaseListView, so let’s look at the run method of CBaseListView first:

Copy code The code is as follows:
public function run()
{
$this->registerClientScript();
echo CHtml::openTag($this->tagName,$this->htmlOptions)."n";
$this->renderKeys();
$this->renderContent();
echo CHtml::closeTag($this->tagName);
}

Please note the first method registerClientScript, this method is implemented in CListView:

Copy code The code is as follows:
public function registerClientScript()
{
……
$cs=Yii::app()->getClientScript();
$cs->registerCoreScript('jquery');
$cs->registerCoreScript('bbq');
……
}

Seeing that jquery and bbp seem to be closer to the truth, let’s look at the CClientScript::registerCoreScript method:

Copy code The code is as follows:
public function registerCoreScript($name)
{
$this->_hasScripts=true;
$this->_coreScripts[$name]=$name;
$params=func_get_args();
$this->recordCachingAction('clientScript','registerCoreScript',$params);
}

This actually mainly records the js to be rendered on the final page, and the actual rendered URL is generated through the getCoreScriptUrl method:

Copy code The code is as follows:
public function getCoreScriptUrl()
{
if($this->_baseUrl!==null)
return $this->_baseUrl;
else
return $this->_baseUrl=Yii::app()->getAssetManager()->publish(YII_PATH.'/web/js/source');
}

Next let’s take a look at the specific process of publish:

Copy code The code is as follows:
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;
……
}

The path is hashed here, so the path we see is irregular, and since the js code of the jq series is all under the same path (all under framework/web/js/source), the hash The values ​​are the same.

In addition, in addition to the above, CAssetManager allows multiple modules to reuse the same code, another benefit of using CAssetManager is security isolation, placing the real code in a protected path and loading it on demand.

I hope this article will be helpful to everyone’s PHP program design based on the yii framework.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/920610.htmlTechArticlePrinciple analysis of Yii core component AssetManager, yiiassetmanager In this article, we use the demo-blog program that comes with yii to analyze the core components of Yii AssetManager, he can automatically load css and javascript,...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template