First, the initialization of the project
Initialize with Composer.
Composer Create-project--prefer-dist yiisoft/yii2-app-basic myapp (or use PHP Composer.phar create-project--prefer-dist Yiisoft/yii2-app-basic MyApp
Second, installation Fontawesome
Still installed through Composer. Search Packagist.org Official Package list, we found the configuration of Fontawesome. will be FortAwesome/Font-Awesome": "*"
added to the project's Composer.json configuration file.
"require": { "PHP":">=5.4.0", "Hybridauth/hybridauth":"Dev-master", "Fortawesome/font-awesome":"*",//<-here "Yiisoft/yii2":"*", "Yiisoft/yii2-swiftmailer":"*", "Yiisoft/yii2-bootstrap":"*", "Yiisoft/yii2-debug":"*", "yiisoft/yii2-gii":"*"},
then run composer update (or PHP composer.phar update) to pull the Fontawesome package from Github to the project locally.
Iii. creating a Fontawesome Resource bundle (Asset bundle)
In order to use these libraries, we need to /assets
create a project directory under theFontAwesomeAsset.php
<?PHPnamespaceApp\assets;use Yii\web\assetbundle;classFontawesomeasset extends assetbundle{//The following resource files are not available in the Web directory and cannot be accessed directly by the browser. So we need//Specifies the SourcePath property. Note @vendor This alias, which represents the Vender directory Public$sourcePath ='@vendor/fortawesome/font-awesome'; Public$css = [ 'Css/font-awesome.css', ];}
To use a resource bundle:
1. Introduce this resource bundle on a specific page
// These two sentences are written directly in the view on that page. Use app\assets\fontawesomeasset; Fontawesomeasset::register ($this);
2. Introduce it globally or as a dependent reference to another resource. Add it to the asset/appasset.php of the project:
<?PHP/** * @linkhttp://www.yiiframework.com/* @copyright Copyright (c) Yii software LLC * @licensehttp://www.yiiframework.com/license/ */namespaceApp\assets;use Yii\web\assetbundle;/** * @author Qiang Xue <[email protected]> * @since 2.0*/classAppasset extends assetbundle{ Public$basePath ='@webroot'; Public$BASEURL ='@web'; Public$css = [ 'Css/site.css', ]; Public$js = [ ]; Public$depends = [ 'Yii\web\yiiasset', 'Yii\bootstrap\bootstrapasset', 'App\assets\fontawesomeasset', ];}
Refresh the page to see the CSS, JS resources that have been introduced (as shown in Google Chrome developer tools)
YII2, adding fontawesome