The day before yesterday, I helped my colleagues change a website bug that was 10 years ago. a bunch of javasderequire on the page can't help but make people cry. According to the discussion on V2EX, writing PHP without a framework is equivalent to playing tricks. YiiFramework is a PHP framework that I have been using for more than two years. it is very active, and I want to talk about how to add external resources (external assets) to the Yii2 project ), take FontAwesome as an example.
Yii2 began to use composer for project dependency management. this product is similar to npm in NodeJS and can automatically obtain the latest third-party libraries on Github (such as Bootstrap, fontAwesome ). After the official tutorial is installed, you can initialize the project.
I. initialize the project
Initialize with Composer
php composer.phar create-project --prefer-dist --stability=dev yiisoft/yii2-app-basic basic
Then start the code. for Model Controller View, click "no table" here.
II. install FontAwesome
Finally, your project needs to reference a third-party library, and we still install it through Composer. Search for the packagist.org official package list and find the FontAwesome configuration. 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 ":"*"},//...
Then run
php composer.phar update
Pull the FontAwesome package from Github to the local project.
3. create a FontAwesome resource package (asset bundle)
To use these libraries, we need to create a FontAwesomeAsset. php file in the/assets Directory of the project.
Namespace assets; use yii \ web \ AssetBundle; class FontAwesomeAsset extends AssetBundle {// The following resource files are not in the web Directory and cannot be directly accessed by the browser. Therefore, we need to // specify the sourcePath attribute. Note that the alias @ vendor indicates the vender directory public $ sourcePath = '@ vendor/fortawesome/font-awesome'; public $ css = ['css/font-awesome.css ',];}
4. register files and introduce resources
There are two methods. First, when you want to introduce this resource package to a specific page
// These two sentences are directly written in the view on that page. use assets \ FontAwesomeAsset; FontAwesomeAsset: register ($ this );
Second, introduce it globally on your website or reference it as the dependency of another resource. Add it to the project's asset/AppAsset. php:
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 package class 'assets \ fontawesomeasset'] here;}
Refresh the page to see if the corresponding css and js resources have been introduced.