Home > Article > Backend Development > Yii core component AssetManager principle analysis, yiiassetmanager_PHP tutorial
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:
The paths of these js files are all in the assets folder. Assets is followed by a folder path that has obviously been hashed. The path of the js code that belongs to jq is the same. Where does this code come from?
Looking directly at the view file, you can’t see any code that introduces js, so it should be introduced using widget:
This widget is also a Zii extension that comes with Yii, so we can find Zii’s CListView code, and CListView inherits CBaseListView, so let’s look at the run method of CBaseListView first:
Please note the first method registerClientScript, this method is implemented in CListView:
Seeing that jquery and bbp seem to be closer to the truth, let’s look at the CClientScript::registerCoreScript method:
This actually mainly records the js to be rendered on the final page, and the actual rendered URL is generated through the getCoreScriptUrl method:
Next let’s take a look at the specific process of publish:
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 and loading it on demand.
I hope this article will be helpful to everyone’s PHP program design based on the yii framework.