1.1 Resource Management (Assets)1.1.1 Basic Instructions
Reference Documentation:
1, http://www.yiichina.com/tutorial/399
2, http://yii2.techbrood.com/guide-assets.html
3, http://www.yiifans.com/yii2/guide/structure-assets.html
Yii2.0 for css/js Management, use the assetbundle Resource Pack class. How do views load css/js on demand ?
use pre-defined resource pack classes in the page, suchas backend/assets/appasset.php, where the CSS/JS isused in the resource bundle. appasset.php Code:
classAppassetextendsAssetbundle { Public$basePath= ' @webroot '; Public$BASEURL= ' @web '; Public$css= [ ' Css/site.css ', ]; Public$js= [ ]; Public$depends= [ ' Yii\web\yiiasset ', ' Yii\bootstrap\bootstrapasset ', ]; } |
Description
$css: A CSS file that introduces a page
$js: JS file that introduces the page
$depends: Introducing a dependent front end library
at load time, the front-end libraries in the $depends load first,$css and the files in the $js. In general,CSS is loadedin at the bottom of <body></body>. the path to the Css/js file is stored in the directory specified by the $basePath parameter, andthe $CSS/$js parameter specifies the relative path. the dependent libraries that are configured in the $depends are loaded first.
using this approach to manage front-end resources, it feels more The way in Yii 1 is much more refreshing, and when used, add code to the view file:
<?php
/ * @var $this \yii\web\view */ /* @var $content String */
UseBackend\assets\appasset; Useyii\helpers\html; UseYii\bootstrap\nav; UseYii\bootstrap\navbar; UseYii\widgets\breadcrumbs; UseCommon\widgets\alert;
Appasset::Register($this); ?> |
1.1.2 Resource Release
in the Yii backend/web and front/web directory, there are assets subdirectories, there are some seemingly random numbers and characters of the directory, which contains some front-end library code, such as jquery,Bootstrap, and so on, these subdirectories are not available until the app starts running, just after some pages have been accessed, what's going on?
This is because some front-end libraries are stored in directories that cannot be accessed directly under the Web, and Yii copies the files of these libraries to the assets directory in order to ensure proper useof the pages .
Which front-end libraries will be copied?
This depends on whether the $sourcePath parameters are configured in the PHP file for the resource bundle , only the $sourcePath parameters are configured , and there is no configuration $basePath and $baseUrl , the front-end library is copied to the assets directory at execution time , and a subdirectory is created with the hash value of the source directory .
What if the resource is updated?
in the Assetmanager->hash () function, it is based on the source directory / file name + update time to do hash , so if the update time has changed, will calculate a new Hash value, and the new hash value represents a directory that does not exist under the assets directory, so a new directory is created and the updated resource file is copied to the new directory.
There are several problems with this:
1, Assets Directory of sub-directories may be more and more, there are many garbage directories;
workaround: Periodically clear subdirectories under assets.
2, update the source directory under the resources in the directory, the source directory update time will not change, resulting in resources will not be published;
Workaround: Clear The subdirectories under assets after publishing.
1.1.3 Common Front End Libraries
in the In Yii 2 , a common front-end library:
library |
version number |
Resource bundle |
|
Bootstrap |
3.3.7 |
bootstrapasset.php |
Vendor\bower\bootstrap\dist |
Jquery |
2.2.4 |
Jqueryasset |
Vendor\bower\jquery\dist |
Note: Bootstrapasset, only introduced the Bootstrap.css, did not introduce Bootstrap.js, is estimated to be performance bar.
This article is from the "Rainman" blog, make sure to keep this source http://lancelot.blog.51cto.com/393579/1871256
Yii 2--Resource Management (Assets)