Home > PHP Framework > YII > How to reference assets in Yii?

How to reference assets in Yii?

coldplay.xixi
Release: 2020-07-16 10:22:33
Original
2879 people have browsed it

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.

How to reference assets in Yii?

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
Copy after login

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/assetsDown.

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(&#39;application.modules.admin.assets&#39;)); 
        return $this->_assetsUrl; 
    } 
   
    public function setAssetsUrl($value) 
    { 
        $this->_assetsUrl=$value; 
    } 
}
Copy after login

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"/>
Copy after login

Force update asset before use

 $baseJsUrl = Yii::app()->getAssetManager()->publish($baseJsPath, false, -1, YII_DEBUG);
Copy after login

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!

Related labels:
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