Home Backend Development PHP Tutorial Principle analysis of Yii core component AssetManager

Principle analysis of Yii core component AssetManager

Jun 19, 2018 pm 02:47 PM
yii analyze principle core components

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>
Copy after login

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(&#39;zii.widgets.CListView&#39;, array(
&#39;dataProvider&#39;=>$dataProvider,
&#39;itemView&#39;=>&#39;_view&#39;,
&#39;template&#39;=>"{items}n{pager}",
));
?>
Copy after login

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);
}
Copy after login

Please pay attention to the first method registerClientScript. This method is implemented in CListView:

public function registerClientScript()
{
……
$cs=Yii::app()->getClientScript();
$cs->registerCoreScript(&#39;jquery&#39;);
$cs->registerCoreScript(&#39;bbq&#39;);
……
}
Copy after login

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(&#39;clientScript&#39;,&#39;registerCoreScript&#39;,$params);
}
Copy after login

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.&#39;/web/js/source&#39;);
}
Copy after login

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;
……
}
Copy after login

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Analysis of the function and principle of nohup Analysis of the function and principle of nohup Mar 25, 2024 pm 03:24 PM

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

Angular components and their display properties: understanding non-block default values Angular components and their display properties: understanding non-block default values Mar 15, 2024 pm 04:51 PM

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.

In-depth discussion of the principles and practices of the Struts framework In-depth discussion of the principles and practices of the Struts framework Feb 18, 2024 pm 06:10 PM

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

In-depth understanding of the batch Insert implementation principle in MyBatis In-depth understanding of the batch Insert implementation principle in MyBatis Feb 21, 2024 pm 04:42 PM

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

Detailed explanation of the principle of MyBatis paging plug-in Detailed explanation of the principle of MyBatis paging plug-in Feb 22, 2024 pm 03:42 PM

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

An in-depth analysis of the functions and working principles of the Linux chage command An in-depth analysis of the functions and working principles of the Linux chage command Feb 24, 2024 pm 03:48 PM

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

Analysis of the reasons why the secondary directory of DreamWeaver CMS cannot be opened Analysis of the reasons why the secondary directory of DreamWeaver CMS cannot be opened Mar 13, 2024 pm 06:24 PM

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

An in-depth discussion of the functions and principles of Linux RPM tools An in-depth discussion of the functions and principles of Linux RPM tools Feb 23, 2024 pm 03:00 PM

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

See all articles