Examples _php examples of assets use in Yii

Source: Internet
Author: User
Tags yii

I. The role of YII assets:

The role of assets in 1.YII is to facilitate modularity, plug-ins, and generally for security reasons do not allow access to the protected files below the URL , but we also want to separate module out, so we need to use the publication, Make a copy of the file under a directory to assets for easy access via URL

$assets = Yii::getpathofalias (' ext '). ' /css ';
$BASEURL = Yii::app ()->getassetmanager ()->publish ($assets);
$BASEURL = Yii::app ()->assetmanager->publish ($assets); EXTENSIONS/CSS publishes to assets
Yii::app ()->clientscript->registercssfile ($baseUrl. ') under the creation of a randomly conflicting folder. /main.css ');//Refer to Main.css below assets

2. If a module needs to add use resources, it can be added directly from the Webroot reference.
But trying to create a module can be referenced anywhere, and resources are independent and can avoid naming conflicts .
How do you guarantee that your file name does not conflict with a file with the same name as an attempt by some scattered applications, as is the case for JS,IMAGES,CSS.
by Cassetmanager,yii::app ()->assetmanager can automatically publish private resources to the public directory Webroot/assets

second, the use of examples:

The following is an example of admin module to illustrate the use of assets:

1, the need to use the resources under the modules/admin/assets.

2, through Cassetmanager,yii::app ()->assetmanager can automatically publish the private resources to the public directory site Directory/assets

3. Yii will automatically create a randomly/assets folder under the Site directory, such as 2b31b42b, and copy the files in your modules/admin/assets directory.
Get the following code, modify the protected\modules\admin\adminmodule.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, using $this->module->assetsurl in/protected/modules/admin/views/layouts/main.php can call your CSS and other files, the code looks like this:

<link rel= "stylesheet" type= "Text/css" href= "<?php echo $this->module->assetsurl;? >/css/screen.css" Rel= "External nofollow"/>

You also need to force an update asset before you use the code 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.