This article mainly introduces the principle analysis of AssetManager, the core component of Yii, and analyzes the principle and implementation process of AssetManager component in more detail, which will help to gain an in-depth understanding of the characteristics of the Yii framework. Friends in need can refer to it
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:
<link rel="stylesheet" type="text/css" href="/yii/demos/blog/assets/d6bb6ebe/highlight.css" /> <link rel="stylesheet" type="text/css" href="/yii/demos/blog/assets/c2e28f0f/pager.css" /> <script type="text/javascript" src="/yii/demos/blog/assets/d6112c6a/jquery.min.js"></script> <script type="text/javascript" src="/yii/demos/blog/assets/d6112c6a/jquery.ba-bbq.js"></script>
The paths of these js files are all in the assets folder, and assets is followed by an obvious The hashed folder path is the same as the path of the js code belonging to jq. Where does this code come from?
Looking directly at the view file, you can’t see any code that introduces js, so you should use widgets Introduced:
<?php $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 first look at the run method of CBaseListView:
public function run() { $this->registerClientScript(); echo CHtml::openTag($this->tagName,$this->htmlOptions)."n"; $this->renderKeys(); $this->renderContent(); echo CHtml::closeTag($this->tagName); }
Please pay attention to the first method registerClientScript. This method is implemented in CListView:
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:
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:
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:
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, press Needs to be loaded.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
About usage analysis of PHP’s custom serialization interface Serializable
About the principles of multi-player module development in PHP
The above is the detailed content of Principle analysis of Yii core component AssetManager. For more information, please follow other related articles on the PHP Chinese website!