Method Analysis of Loading CSS and JS Files in Yii2 Framework

Time:2019-10-8

This paper illustrates the method of loading CSS and JS files by Yii2 framework. Share for your reference, as follows:

1. The first step is to put our CSS and JS files in the web directory.

Step 2: Modify the assets/AppAsset.php file

<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
namespace app\assets;
use yii\web\AssetBundle;
/**
 * @author Qiang Xue <[email protected]>
 * @since 2.0
 */
class AppAsset extends AssetBundle
{
  public $basePath = '@webroot';
  public $baseUrl = '@web';
  // public $css = [
  //   'css/site.css',
  //   'bootstrap/css/bootstrap.min.css',
  // ];
  // public $js = [
  //   'jquery/jquery.min.js',
  //   'bootstrap/js/bootstrap.min.js'
  // ];
  public function registerAssetFiles( $view )
  {
    // Add a version number
    // Purpose: To enable browsers to access the latest files
    $release = "20170226";
    $this->css = [
      "bootstrap/css/bootstrap.min.css?v={$release}"
    ];
    $this->js = [
       'jquery/jquery.min.js',
       'bootstrap/js/bootstrap.min.js'
    ];
    parent::registerAssetFiles( $view );
  }
}

We recommend the following two loading methods.

Readers interested in Yii-related content can see the topics of this site: Introduction to Yii Framework and Common Skills, Summary of Excellent Development Framework of php, Basic Course of Introduction to Smrty Template, Introduction to Php Object-Oriented Programming, Summary of Usage of Php String, Introduction to php+mysql Database Operations, and Common Database Operations of php. Summary of Skills

I hope that this article will be helpful to the PHP program design based on Yii framework.