Home > Article > PHP Framework > What does yii assets mean?
The assets folder generally stores some style files (css), script files (js), picture files (images), etc. in the front and backend, so it can be used with Yii::app()->request ->baseUrl is used together (to get the project name).
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 If you want to separate the module, you need to use publishing, that is, copy the files in a directory to assets for easy access through url (recommended learning: yii tutorial)
$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
If a module If you need to add resources, you can 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
The following takes the admin module as an example
1. Place the resources you need under modules/admin/assets.
2. Then through CAssetManager, Yii::app()->assetManager can automatically publish the private resources to the public directory website directory/assets
3. Yii will automatically Create a random non-conflicting folder under the /assets directory, such as 2b31b42b, and copy the files in your modules/admin/assets directory to it.
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, use $ in /protected/modules/admin/views/layouts/main.php this->module->assetsUrl can call your css and other files.
The above is the detailed content of What does yii assets mean?. For more information, please follow other related articles on the PHP Chinese website!