Yii method of citing assets: first put the resources you need to use under [modules/admin/assets]; then publish the private resources to the public directory; then put them under [/assets] in the website directory Create a random, non-conflicting folder; then copy the files.
Yii reference assets method:
Why use YII assets
1.The role of assets is to facilitate modularization and plug-in. Generally speaking, for security reasons, access to files under protected through URL is not allowed, but we also want to separate the module, so we need to use publishing, that is, a directory Copy the file below to assets for easy access through url
$assets = Yii::getPathOfAlias('ext').'/css'; //$baseUrl = Yii::app()->getAssetManager()->publish($assets); $baseUrl = Yii::app()->assetManager->publish($assets); //extensions/css发布到assets的创建一个随机不冲突的文件夹下 Yii::app()->clientScript->registerCssFile($baseUrl.'/main.css');//引用assets下面的main.css
2. If a module needs to add resources, just reference and add them directly from webroot.
But try to create a module that can be referenced anywhere, has independent resources and avoids naming conflicts.
How do you ensure that your file names don't conflict with some scattered applications trying to use files with the same name, the same for js, images, css.
Through CAssetManager, Yii::app()->assetManager can automatically publish private resources to the public directory webroot/assets
Related learning recommendations: yii Tutorial
The following takes the admin module as an example
1. Put the resources you need in modules/admin/assets
Down.
2. Then through CAssetManager, Yii::app()->assetManager can automatically publish private resources to the public directory, website directory/assets
3. Yii will automatically Create a random non-conflicting folder under /assets
of the website directory, such as 2b31b42b, and copy the files in your modules/admin/assets
directory.
Obtained through the following code, modify the protected\modules\admin\AdminModule.php
file,
<?php class AdminModule extends CWebModule { private $_assetsUrl; public function getAssetsUrl() { if($this->_assetsUrl===null) $this->_assetsUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('application.modules.admin.assets')); return $this->_assetsUrl; } public function setAssetsUrl($value) { $this->_assetsUrl=$value; } }
Then, in /protected/modules/admin/views/layouts/ Use $this->module->assetsUrl in main.php to call your css and other files.
<link rel="stylesheet" type="text/css" href="<?php echo $this->module->assetsUrl; ?>/css/screen.css"/>
Force update asset before use
$baseJsUrl = Yii::app()->getAssetManager()->publish($baseJsPath, false, -1, YII_DEBUG);
The above is the detailed content of How to reference assets in Yii?. For more information, please follow other related articles on the PHP Chinese website!