Yii2 uses a more standard method to introduce js and css files through the AppAsset: register ($ this) method. In the Yii2 example, layouts main. php has its usage: AppAsset: register ($ this ).
There is an assets directory in the WEB directory, which contains an Appasset. Php file with the following content:
Namespace frontend \ assets;
Use yii \ web \ AssetBundle;
/**
* @ Author Qiang Xue <qiang.xue@gmail.com>
* @ Since 2.0
*/
Class AppAsset extends AssetBundle
{
Public $ basePath = '@ webroot ';
Public $ baseUrl = '@ web ';
Public $ css = [
'Css/site.css ',
];
Public $ js = [
];
Public $ depends = [
'Yii \ web \ yiiasset ',
'Yii \ bootstrap \ bootstrapasset ',
];
}
This AppAsset class inherits Yii \ web \ AssetBundle, which mainly defines the paths and dependencies of js and css files.
In the Template layout file main. php, use AppAsset: register ($ this) to register these css and js files. In addition, add the following in the html head:
<? Php $ this-> head ()?>
This statement generates a replacement character, indicating that the reference code of css and js is displayed here. Don't forget to add this sentence to the head.