Table of Contents
Examples of using assets in YII, examples of yiiassets
How to publish css, js, and image to assets in yii framework
Simple example of CRUD operation in php yii framework database
Home Backend Development PHP Tutorial Examples of using assets in YII, yiiassets examples_PHP tutorial

Examples of using assets in YII, yiiassets examples_PHP tutorial

Jul 13, 2016 am 10:21 AM
assets yii

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

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, 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;
 }
}

Copy after login

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" />

Copy after login

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

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

vue3+vite assets dynamically introduce images and solve the problem of incorrect image path not being displayed after packaging vue3+vite assets dynamically introduce images and solve the problem of incorrect image path not being displayed after packaging May 10, 2023 pm 05:55 PM

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

How to use Yii3 framework in php? How to use Yii3 framework in php? May 31, 2023 pm 10:42 PM

How to use Yii3 framework in php?

How to use PHP framework Yii to develop a highly available cloud backup system How to use PHP framework Yii to develop a highly available cloud backup system Jun 27, 2023 am 09:04 AM

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? Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Jun 19, 2023 am 08:09 AM

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

Data query in Yii framework: access data efficiently Data query in Yii framework: access data efficiently Jun 21, 2023 am 11:22 AM

Data query in Yii framework: access data efficiently

Symfony vs Yii2: Which framework is better for developing large-scale web applications? Symfony vs Yii2: Which framework is better for developing large-scale web applications? Jun 19, 2023 am 10:57 AM

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 How to convert yii objects into arrays or directly output to json format Jan 08, 2021 am 10:13 AM

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

Yii2 Programming Guide: How to run Cron service Yii2 Programming Guide: How to run Cron service Sep 01, 2023 pm 11:21 PM

Yii2 Programming Guide: How to run Cron service

See all articles