search
HomeBackend DevelopmentPHP TutorialPrinciple analysis of Yii core component AssetManager
Principle analysis of Yii core component AssetManagerJun 19, 2018 pm 02:47 PM
yiianalyzeprinciplecorecomponents

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(&#39;zii.widgets.CListView&#39;, array(
&#39;dataProvider&#39;=>$dataProvider,
&#39;itemView&#39;=>&#39;_view&#39;,
&#39;template&#39;=>"{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(&#39;jquery&#39;);
$cs->registerCoreScript(&#39;bbq&#39;);
……
}

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);
}

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;);
}

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!

Statement
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
如何在 Windows 11、10 中启用或禁用核心隔离内存完整性如何在 Windows 11、10 中启用或禁用核心隔离内存完整性Apr 27, 2023 pm 10:43 PM

如今,大多数Windows用户都使用虚拟机。当他们系统上的核心隔离被禁用时,安全风险和攻击是可以预料的。即使设置了核心隔离,如果用户升级了系统,也会禁用内存完整性。如果启用核心隔离,系统将免受攻击。对于经常使用虚拟计算机的人,强烈建议他们启用它。如果您正在寻求有关如何在任何Windows11系统上启用或禁用核心隔离内存完整性的说明,此页面可以提供帮助。如何使用Windows安全应用在Windows11中启用或禁用核心隔离内存完整性第1步:按Windows键并键入Windows安全

如何使用PHP框架Yii开发一个高可用的云备份系统如何使用PHP框架Yii开发一个高可用的云备份系统Jun 27, 2023 am 09:04 AM

随着云计算技术的不断发展,数据的备份已经成为了每个企业必须要做的事情。在这样的背景下,开发一款高可用的云备份系统尤为重要。而PHP框架Yii是一款功能强大的框架,可以帮助开发者快速构建高性能的Web应用程序。下面将介绍如何使用Yii框架开发一款高可用的云备份系统。设计数据库模型在Yii框架中,数据库模型是非常重要的一部分。因为数据备份系统需要用到很多的表和关

Yii2 vs Phalcon:哪个框架更适合开发显卡渲染应用?Yii2 vs Phalcon:哪个框架更适合开发显卡渲染应用?Jun 19, 2023 am 08:09 AM

在当前信息时代,大数据、人工智能、云计算等技术已经成为了各大企业关注的热点。在这些技术中,显卡渲染技术作为一种高性能图形处理技术,受到了越来越多的关注。显卡渲染技术被广泛应用于游戏开发、影视特效、工程建模等领域。而对于开发者来说,选择一个适合自己项目的框架,是一个非常重要的决策。在当前的语言中,PHP是一种颇具活力的语言,一些优秀的PHP框架如Yii2、Ph

php如何使用Yii3框架?php如何使用Yii3框架?May 31, 2023 pm 10:42 PM

随着互联网的不断发展,Web应用程序开发的需求也越来越高。对于开发人员而言,开发应用程序需要一个稳定、高效、强大的框架,这样可以提高开发效率。Yii是一款领先的高性能PHP框架,它提供了丰富的特性和良好的性能。Yii3是Yii框架的下一代版本,它在Yii2的基础上进一步优化了性能和代码质量。在这篇文章中,我们将介绍如何使用Yii3框架来开发PHP应用程序。

Yii框架中的数据查询:高效地访问数据Yii框架中的数据查询:高效地访问数据Jun 21, 2023 am 11:22 AM

Yii框架是一个开源的PHPWeb应用程序框架,提供了众多的工具和组件,简化了Web应用程序开发的流程,其中数据查询是其中一个重要的组件之一。在Yii框架中,我们可以使用类似SQL的语法来访问数据库,从而高效地查询和操作数据。Yii框架的查询构建器主要包括以下几种类型:ActiveRecord查询、QueryBuilder查询、命令查询和原始SQL查询

Symfony vs Yii2:哪个框架更适合开发大型Web应用?Symfony vs Yii2:哪个框架更适合开发大型Web应用?Jun 19, 2023 am 10:57 AM

随着Web应用需求的不断增长,开发者们在选择开发框架方面也越来越有选择的余地。Symfony和Yii2是两个备受欢迎的PHP框架,它们都具有强大的功能和性能,但在面对需要开发大型Web应用时,哪个框架更适合呢?接下来我们将对Symphony和Yii2进行比较分析,以帮助你更好地进行选择。基本概述Symphony是一个由PHP编写的开源Web应用框架,它是建立

yii如何将对象转化为数组或直接输出为json格式yii如何将对象转化为数组或直接输出为json格式Jan 08, 2021 am 10:13 AM

yii框架:本文为大家介绍了yii将对象转化为数组或直接输出为json格式的方法,具有一定的参考价值,希望能够帮助到大家。

Yii2编程指南:运行Cron服务的方法Yii2编程指南:运行Cron服务的方法Sep 01, 2023 pm 11:21 PM

如果您问“Yii是什么?”查看我之前的教程:Yii框架简介,其中回顾了Yii的优点,并概述了2014年10月发布的Yii2.0的新增功能。嗯>在这个使用Yii2编程系列中,我将指导读者使用Yii2PHP框架。在今天的教程中,我将与您分享如何利用Yii的控制台功能来运行cron作业。过去,我在cron作业中使用了wget—可通过Web访问的URL来运行我的后台任务。这引发了安全问题并存在一些性能问题。虽然我在我们的启动系列安全性专题中讨论了一些减轻风险的方法,但我曾希望过渡到控制台驱动的命令

See all articles

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools