This time to say, how to add external resources (external assets) to the YII2 project, Fontawesome as an example.
Yii2 started using composer to do the project's dependency management, which is similar to the NodeJS inside NPM, can automatically get the latest version of Github on the third-party library (such as Bootstrap, Fontawesome, etc.). After you've installed the official tutorial, you're ready to start initializing the project.
First, the initialization of the project
Initialize with Composer.
PHP composer.phar create-project--prefer-dist--stability=dev yiisoft/yii2-app-basic Basic
Then start code, Model Controller View god horse, here press no table.
Second, installation Fontawesome
Finally, your project has evolved to refer to third-party libraries, and we still install them through Composer. Search Packagist.org Official Package list, we found the configuration of Fontawesome. Add Fortawesome/font-awesome ":" * 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 ":" * "},//...
And then run
PHP Composer.phar Update
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 create a fontawesomeasset.php in the/assets directory of the Project
Namespace Assets;use yii\web\assetbundle;class Fontawesomeasset extends assetbundle{//The following resource files are not available in the Web directory and cannot be accessed directly by the browser. So we need//Specify the SourcePath property. Note @vendor This alias, indicates that the vender directory public $sourcePath = ' @vendor/fortawesome/font-awesome '; Public $css = [ ' css/font-awesome.css ',];}
Iv. registration documents, introduction of resources
There are two ways. First, when you want to introduce this resource bundle on a particular page
These two sentences are written directly in the view of that page use Assets\fontawesomeasset; Fontawesomeasset::register ($this);
The second is to introduce it globally on your site, or as a dependency reference to another resource. Add it to the asset/appasset.php of the project:
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 ', //Add our Fontawesomeasset bag class here ' Assets\fontawesomeasset '];}
Refresh the page to see if the corresponding CSS and JS resources have been introduced.
http://www.bkjia.com/PHPjc/825535.html www.bkjia.com true http://www.bkjia.com/PHPjc/825535.html techarticle this time to say, how to add external resources (external assets) to the YII2 project, Fontawesome as an example. Yii2 began to use composer to do the project's dependency management, this goods is class ...