Home >PHP Framework >YII >How does the Yii framework reference JS and CSS files?

How does the Yii framework reference JS and CSS files?

Guanhui
GuanhuiOriginal
2020-06-04 11:57:492705browse

How does the Yii framework reference JS and CSS files?

#How does the Yii framework reference JS and CSS files?

First put the CSS and JS files in the web directory; then open the "AppAsset.php" file and rewrite the "registerAssetFiles" method; then change the CSS and JS file paths in this method Register; finally call the parent method.

Sample code

<?php
/**
 * @link http://www.yiiframework.com/
 * @license http://www.yiiframework.com/license/
 */
namespace app\assets;
use yii\web\AssetBundle;



class AppAsset extends AssetBundle
{
  public $basePath = &#39;@webroot&#39;;
  public $baseUrl = &#39;@web&#39;;
  // public $css = [
  //   &#39;css/site.css&#39;,
  //   &#39;bootstrap/css/bootstrap.min.css&#39;,
  // ];
  // public $js = [
  //   &#39;jquery/jquery.min.js&#39;,
  //   &#39;bootstrap/js/bootstrap.min.js&#39;
  // ];
  public function registerAssetFiles( $view )
  {
    //加一个版本号
    //目的:使浏览器获取最新的文件
    $release = "20200526";
    $this->css = [
      "bootstrap/css/bootstrap.min.css?v={$release}"
    ];
    $this->js = [
       &#39;jquery/jquery.min.js&#39;,
       &#39;bootstrap/js/bootstrap.min.js&#39;
    ];
    parent::registerAssetFiles( $view );
  }
}

Recommended tutorial: "Yii Tutorial"

The above is the detailed content of How does the Yii framework reference JS and CSS files?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn