Examples of use of assets in Yii, yiiassets example _php tutorial

Source: Internet
Author: User
Tags yii

Examples of use of assets in Yii, yiiassets example


First, the role of YII assets:

1.yii in the role of assets is convenient for modular, plug-in, generally for security reasons do not allow the URL to access protected the following file , but we want to separate the module, so we need to use the release, A copy of the file under one directory is copied to assets below for easy access via URL

$assets = Yii::getpathofalias (' ext '). ' /css ';//$baseUrl = Yii::app ()->getassetmanager ()->publish ($assets); $baseUrl = Yii::app ()->assetmanager- >publish ($assets); Extensions/css published to assets creates a random, non-conflicting folder under Yii::app ()->clientscript->registercssfile ($BASEURL. /main.css ');//reference assets below the MAIN.CSS

2. If a module needs to add a resource, add it directly from the Webroot.
However, 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 will not conflict with some fragmented application that attempts to use the same name for Js,images,css?
by Cassetmanager,yii::app ()->assetmanager, you can automatically publish private resources to a common directory Webroot/assets

Second, use example:

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 the Cassetmanager,yii::app ()->assetmanager can automatically publish the private resources to the public directory of the site Directory/assets

3. Yii will automatically create a random and non-conflicting folder under the/assets of the site directory, such as 2b31b42b, and copy the files from your Modules/admin/assets directory in the past.
Get the following code, 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, using $this->module->assetsurl in/protected/modules/admin/views/layouts/main.php, you can call your CSS and other files, as shown in the following code:

 
  

You will also need to force the update asset before using the code as follows:

$BASEJSURL = Yii::app ()->getassetmanager ()->publish ($baseJsPath, False,-1, yii_debug);

How the YII framework publishes CSS, JS, and image to assets

But in the spirit of solving the problem, it is found that assets is a good stuff. $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: Location of files such as CSS, $ Hashbyname whether the resource folder generated under the Assets folder is hashed, $level load mode (single file, folder full load God horse), $forceCopy whether to force the reload every time (I think CSS debugging is better, Otherwise, the modified CSS may not take effect immediately because it does not reload.

PHP yii Framework Database crud Operation Simple Example

Www.yiiframework.com/...st-app
When you're done, you've got a basic idea of how to get started.

http://www.bkjia.com/PHPjc/854354.html www.bkjia.com true http://www.bkjia.com/PHPjc/854354.html techarticle The use example of assets in Yii, yiiassets example One, Yii assets role: the role of assets in 1.yii is convenient for modular, plug-in, generally for security reasons not allowed to pass ...

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