


Examples of using assets in YII, yiiassets examples_PHP tutorial
Jul 13, 2016 am 10:21 AMExamples of using assets in YII, examples of yiiassets
1. The role of YII assets:
1. The role of assets in yii is to facilitate modularization and plug-inization. Generally speaking, for security reasons, access to files under protected through URL is not allowed , but we also want to separate the module , so you need to use publishing, that is, 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
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, is resource independent and avoids naming conflicts .
How do you ensure that your file names don't conflict with some scattered application's files trying to use the same name, the same goes for js, images, css.
Through CAssetManager, Yii::app()->assetManager can automatically publish private resources to the public directory webroot/assets
2. Usage examples:
The following uses the admin module as an example to illustrate the usage of assets:
1. Place the resources you need under modules/admin/assets.
2. 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 in the website directory, such as 2b31b42b, and copy the files in your modules/admin/assets directory.
Obtained through the following code, modify the protectedmodulesadminAdminModule.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 $this->module->assetsUrl in /protected/modules/admin/views/layouts/main.php to call your css and other files. The code is as follows:
<link rel="stylesheet" type="text/css" href="<?php echo $this->module->assetsUrl; ?>/css/screen.css" rel="external nofollow" />
You also need to forcefully update the asset before use. The code is as follows:
$baseJsUrl = Yii::app()->getAssetManager()->publish($baseJsPath, false, -1, YII_DEBUG);
But in the spirit of solving problems, I found assets to be a good thing. $cs=Yii::app()->clientScript; $cssDir=Yii::getPathOfAlias("access.css") ; $published = Yii::app()->assetManager->publish($cssDir) ; $cssFile = $published . "/" . "homemain.css"; if (!Yii::app()->clientScript->isCssFileRegistered($cssFile)) { Yii::app()->clientScript ->registerCssFile($cssFile); } ?>2. The assetmanager used above is described in /web/CAssetManager.php, where the publish function has the following parameters: $path: the location of css and other files, whether $hashByName Hash the resource folder generated under the assets folder, $level loading method (single file, full folder loading is awesome), whether $forceCopy forces reloading every time (I personally think that comparison is used when debugging css Good, otherwise the modified css may not take effect immediately because it is not reloaded).
www.yiiframework.com/...st-app
After reading it and understanding it, you are basically a beginner

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

vue3+vite assets dynamically introduce images and solve the problem of incorrect image path not being displayed after packaging

How to use PHP framework Yii to develop a highly available cloud backup system

Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications?

Data query in Yii framework: access data efficiently

Symfony vs Yii2: Which framework is better for developing large-scale web applications?

How to convert yii objects into arrays or directly output to json format

Yii2 Programming Guide: How to run Cron service
