_php Example of assetmanager principle analysis of Yii core component

Source: Internet
Author: User
Tags tagname
In this paper, we analyze the Yii core component Assetmanager through the Demo-blog program of Yii, he can automatically load the CSS and JavaScript, and only need one sentence of code. The specific analysis is as follows:

Open the first page of the blog, you will see the following HTML code to introduce JS:
Copy the Code code as follows:


The paths of these JS files are in the assets folder, followed by an apparently hashed folder path, the same path as the JS code belonging to JQ, where does this code come from?

Looking directly at the view file does not see any code that introduces JS, so it should be introduced using widgets:

Copy the Code code as follows: <?php
$this->widget (' Zii.widgets.CListView ', Array (
' Dataprovider ' = $dataProvider,
' Itemview ' = ' _view ',
' Template ' = ' {Items}n{pager} ',
));
?>

This widget is also Yii's own zii extension, so we can find zii CListView code, and CListView is inherited Cbaselistview, so first look at the Cbaselistview of the Run method:

Copy the Code Code as follows: Public function run ()
{
$this->registerclientscript ();
Echo Chtml::opentag ($this->tagname, $this->htmloptions). " n ";
$this->renderkeys ();
$this->rendercontent ();
Echo Chtml::closetag ($this->tagname);
}

Note that the first method, Registerclientscript, is implemented in CListView:

Copy the Code Code 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, the next step is to look at the Cclientscript::registercorescript method:

Copy the Code Code as follows: Public function Registercorescript ($name)
{
$this->_hasscripts=true;
$this->_corescripts[$name]= $name;
$params =func_get_args ();
$this->recordcachingaction (' ClientScript ', ' registercorescript ', $params);
}

Here in fact, the main is to record the final page to render the JS, and actually generate the URL of render is through the Getcorescripturl method:

Copy the Code Code 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 ');
}

Let's look at the specific process of publish:

Copy the Code Code 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;
......
}

This is done by hashing the path, so we see the path is irregular, and because the JQ series of JS Code are all in the same path (all under Framework/web/js/source), so the hash value is the same.

Also, in addition to the above, Cassetmanager allows multiple modules to be reused with the same code system, another benefit of using Cassetmanager is security isolation, placing the real code under a protected path and loading on demand.

It is hoped that this article is helpful to the PHP program design based on YII framework.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.