Yii2安装前端资源包

官方文档也有介绍:http://www.yiichina.com/doc/guide/2.0/structure-assets#bower-npm-assets

安装 iziToast

  1. 安装 iziToast.js 这个前端资源 composer require bower-asset/izitoast 或者添加composer.json然后 update:"bower-asset/izitoast": "^1.1",安装之后大概目录结构如下:

iziToast

  1. 新建IziToastAsset.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php
namespace app\assets;

use yii\web\AssetBundle;
class IziToastAsset extends AssetBundle
{
    public $sourcePath = '@bower/izitoast/dist';
    public $js = [
        'js/iziToast.min.js'
        // more plugin Js here
    ];
    public $css = [
        'css/iziToast.min.css'
        // more plugin CSS here
    ];
}
  1. view层使用:\app\assets\IziToastAsset::register($this)

安装 semantic ui

  1. bower install semantic-ui 或者 composer require bower-asset/semantic-ui(可能是npm-asset/semantic-ui) 会安装在vendor/bower目录下
  2. 创建一个assets资源,比如在 /frontend/assets 下创建一个SemanticAsset.php文件
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<?php
namespace frontend\assets;
use yii\web\AssetBundle;
class SemanticAsset extends AssetBundle
{
    public $sourcePath = '@bower/semantic';
   public $css = [
    'dist/semantic.min.css'
];
public $js = [
  'dist/semantic.min.js'
];
public $depends = [
 'yii\web\YiiAsset',
'yii\bootstap\BootstrapAsset'
]
}
  1. 注册资源,main.php中使用
1
2
3
4
5
<?php
// views/layouts/main.php

use frontend\assets\SemanticAsset;
SemanticAsset::register($this);