这篇文章主要介绍了YII中assets的使用示例,需要的朋友可以参考下
一、YII assets的作用:
1.yii中assets的作用是方便模块化,插件化的,一般来说出于安全原因不允许通过url访问protected下面的文件 ,,但是我们又希望将module单独出来,所以需要使用发布,即将一个目录下的文件复制一份到assets下面方便通过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.如果一个模块需要添加使用资源,直接从webroot中引用添加即可。
但是试图创建一个模块能够在任何地方引用,且资源独立并能够避免命名冲突 。
你如何保证你的文件名不会与一些零散的应用程序的尝试使用相同名称的文件冲突,对于js,images,css也一样。
通过CAssetManager,Yii::app()->assetManager能够自动的将私有资源publish到公共目录下webroot/assets
二、使用示例:
下面以admin module为例说明assets的用法:
1、把需要用到的资源放在modules/admin/assets下。
2、通过 CAssetManager,Yii::app()->assetManager能够自动的将私有资源publish到公共目录下 网站目录/assets
3、Yii会自动在网站目录的/assets下创建一个随机不冲突的文件夹,如2b31b42b,并把你的modules/admin/assets目录下的文件拷贝过去。
通过如下代码获得,修改protected\modules\admin\AdminModule.php文件,
_assetsUrl===null) $this->_assetsUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('application.modules.admin.assets')); return $this->_assetsUrl; } public function setAssetsUrl($value) { $this->_assetsUrl=$value; } }
然后,在/protected/modules/admin/views/layouts/main.php中使用$this->module->assetsUrl就可以调用你的css等文件了,代码如下所示:
使用前还需要强制更新asset,代码如下:
$baseJsUrl = Yii::app()->getAssetManager()->publish($baseJsPath, false, -1, YII_DEBUG);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use