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