Principle analysis of Yii core component AssetManager
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!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



Analysis of the role and principle of nohup In Unix and Unix-like operating systems, nohup is a commonly used command that is used to run commands in the background. Even if the user exits the current session or closes the terminal window, the command can still continue to be executed. In this article, we will analyze the function and principle of the nohup command in detail. 1. The role of nohup: Running commands in the background: Through the nohup command, we can let long-running commands continue to execute in the background without being affected by the user exiting the terminal session. This needs to be run

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

Principle analysis and practical exploration of the Struts framework. As a commonly used MVC framework in JavaWeb development, the Struts framework has good design patterns and scalability and is widely used in enterprise-level application development. This article will analyze the principles of the Struts framework and explore it with actual code examples to help readers better understand and apply the framework. 1. Analysis of the principles of the Struts framework 1. MVC architecture The Struts framework is based on MVC (Model-View-Con

MyBatis is a popular Java persistence layer framework that is widely used in various Java projects. Among them, batch insertion is a common operation that can effectively improve the performance of database operations. This article will deeply explore the implementation principle of batch Insert in MyBatis, and analyze it in detail with specific code examples. Batch Insert in MyBatis In MyBatis, batch Insert operations are usually implemented using dynamic SQL. By constructing a line S containing multiple inserted values

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

The chage command in the Linux system is a command used to modify the password expiration date of a user account. It can also be used to modify the longest and shortest usable date of the account. This command plays a very important role in managing user account security. It can effectively control the usage period of user passwords and enhance system security. How to use the chage command: The basic syntax of the chage command is: chage [option] user name. For example, to modify the password expiration date of user "testuser", you can use the following command

Title: Analysis of the reasons and solutions for why the secondary directory of DreamWeaver CMS cannot be opened. Dreamweaver CMS (DedeCMS) is a powerful open source content management system that is widely used in the construction of various websites. However, sometimes during the process of building a website, you may encounter a situation where the secondary directory cannot be opened, which brings trouble to the normal operation of the website. In this article, we will analyze the possible reasons why the secondary directory cannot be opened and provide specific code examples to solve this problem. 1. Possible cause analysis: Pseudo-static rule configuration problem: during use

The RPM (RedHatPackageManager) tool in Linux systems is a powerful tool for installing, upgrading, uninstalling and managing system software packages. It is a commonly used software package management tool in RedHatLinux systems and is also used by many other Linux distributions. The role of the RPM tool is very important. It allows system administrators and users to easily manage software packages on the system. Through RPM, users can easily install new software packages and upgrade existing software
