Home > PHP Framework > YII > What is the folder assets in Yii?

What is the folder assets in Yii?

青灯夜游
Release: 2019-12-23 17:36:22
Original
5838 people have browsed it

What is the folder assets in Yii?

#What is the assets folder in yii?

Assets are generally folders that store some style files (css), script files (js), picture files (images) and other content in the front and backend, so they can be used with Yii::app() ->request->baseUrl is used together (to obtain the project name).

The role of assets is to facilitate modularization and plug-in. Generally speaking, for security reasons, access to the files under protected through URL is not allowed, but we also want to separate the module, so we need to use release, which will be Copy a file in a directory 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
Copy after login

If a module needs to add resources, just add them directly from the 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(&#39;application.modules.admin.assets&#39;));  
        return $this->_assetsUrl;  
    }  
  
    public function setAssetsUrl($value)  
    {  
        $this->_assetsUrl=$value;  
    }  
}
Copy after login

Then, use $ in /protected/modules/admin/views/layouts/main.php this->module->assetsUrl can call your css and other files.

Recommended learning: yii tutorial

The above is the detailed content of What is the folder assets in Yii?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
yii
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template