Tips for Yii2 use Composer to add FontAwesome font resource _ php instance

Source: Internet
Author: User
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 practical. Yii2 is still in Beta, but it does not affect pre-research. How to add external resources (external assets) to the Yii2 project, using 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.