This article describes how to use assets in YII. For more information, see
I. Role of YII assets:
1. the role of assets in yii is to facilitate modularization and plug-ins. Generally, users are not allowed to access files under protected through URLs for security reasons, but we want to separate the module, therefore, you need to use Publishing to copy files in a directory to assets for convenient url access.
$ Assets = Yii: getPathOfAlias ('text '). '/css'; // $ baseUrl = Yii: app ()-> getAssetManager ()-> publish ($ assets); $ baseUrl = Yii: app () -> assetManager-> publish ($ assets); // extensions/css is released to the assets to create a random, non-conflicting folder. Yii: app () -> clientScript-> registerCssFile ($ baseUrl. '/main.css'); // reference main.css under assets
2. if a module needs to add and use resources, add them directly from webroot.
However, it tries to create a module that can be referenced anywhere and the resource is independent and can avoid name conflicts.
How do you ensure that your file name does not conflict with files with the same name tried by some scattered applications? The same applies to js, images, and css.
You can use CAssetManager, Yii: app ()-> assetManager to automatically publish private resources to webroot/assets under the Public Directory.
II. example:
The following uses the admin module as an example to describe how to use assets:
1. place the required resources under modules/admin/assets.
2. you can use CAssetManager, Yii: app ()-> assetManager to automatically publish private resources to the website directory/assets under the Public Directory.
3. Yii will automatically create a random non-conflicting folder under/assets under the website directory, such as 2b31b42b, and copy the files under your modules/admin/assets directory.
Run the following code to modify the protected \ modules \ admin \ AdminModule. php file,
<?phpclass 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/main. use $ this-> module-> assetsUrl in php to call your css and other files. the code is as follows:
You also need to force update asset before use. the code is as follows:
$baseJsUrl = Yii::app()->getAssetManager()->publish($baseJsPath, false, -1, YII_DEBUG);