Example of assets in YII

Source: Internet
Author: User
This article describes how to use assets in YII. For more information, see I. Role of YII assets:

1. the role of assets in yii is to facilitate modularization and plug-ins. Generally, users are not allowed to access files under protected through URLs for security reasons, but we want to separate the module, therefore, you need to use Publishing to copy files in a directory to assets for convenient url access.

$ Assets = Yii: getPathOfAlias ('text '). '/css'; // $ baseUrl = Yii: app ()-> getAssetManager ()-> publish ($ assets); $ baseUrl = Yii: app () -> assetManager-> publish ($ assets); // extensions/css is released to the assets to create a random, non-conflicting folder. Yii: app () -> clientScript-> registerCssFile ($ baseUrl. '/main.css'); // reference main.css under assets

2. if a module needs to add and use resources, add them directly from webroot.
However, it tries to create a module that can be referenced anywhere and the resource is independent and can avoid name conflicts.
How do you ensure that your file name does not conflict with files with the same name tried by some scattered applications? The same applies to js, images, and css.
You can use CAssetManager, Yii: app ()-> assetManager to automatically publish private resources to webroot/assets under the Public Directory.

II. example:

The following uses the admin module as an example to describe how to use assets:

1. place the required resources under modules/admin/assets.

2. you can use CAssetManager, Yii: app ()-> assetManager to automatically publish private resources to the website directory/assets under the Public Directory.

3. Yii will automatically create a random non-conflicting folder under/assets under the website directory, such as 2b31b42b, and copy the files under your modules/admin/assets directory.
Run the following code to modify the protected \ modules \ admin \ AdminModule. php file,

<?phpclass 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, in/protected/modules/admin/views/layouts/main. use $ this-> module-> assetsUrl in php to call your css and other files. the code is as follows:

 

You also need to force update asset before use. the code is as follows:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.