Home  >  Article  >  Backend Development  >  Examples of using assets in YII, yiiassets examples_PHP tutorial

Examples of using assets in YII, yiiassets examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:21:44857browse

Examples 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,

<&#63;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="<&#63;php echo $this->module->assetsUrl; &#63;>/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);

How to publish css, js, and image to assets in yii framework

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).

Simple example of CRUD operation in php yii framework database

www.yiiframework.com/...st-app
After reading it and understanding it, you are basically a beginner

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/854354.htmlTechArticleUsage examples of assets in YII, yiiassets example 1. The role of YII assets: 1. The role of assets in yii is Conveniently modular and plug-in, generally not allowed for security reasons...
Statement:
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