Home » Php » php – yii2: How to work with font awesome icons?

php – yii2: How to work with font awesome icons?

Posted by: admin July 12, 2020 Leave a comment


I have this HTML link tag that I need to generate using yii\helpers\Html

<a href="register" class="btn btn-black" title="Sign Up"><i class="fa fa-fw fa-user"></i> Sign Up</a>

I am able to do it using method a() but I do not know how to include the font awesome class. Here is the code that I already have using a() method

<?= Html::a('Sign Up',['site/signup'], ['class' => 'btn btn-black', 'title' => 'Sign Up']) ?>

I am using bootstrap for my CSS. Any help would be appreciated.

How to&Answers:

It’s simple

<?= Html::a('<i class="fa fa-fw fa-user"></i> Sign Up',['site/signup'], ['class' => 'btn btn-black', 'title' => 'Sign Up']) ?>


Following code generate your desired HTML.

<?= Html::a(Html::tag('i', '', ['class' => 'fa fa-fw fa-user']) . ' Sign Up ', ['site/signup'], ['class' => 'btn btn-black', 'title' => 'Sign Up']) ?>


You can also use yii2-icons, see: https://github.com/kartik-v/yii2-icons

At first you have to install the extension:

composer require kartik-v/yii2-icons "@dev"

Then you can display the icons e.g. in the following way:

use kartik\icons\Icon;
Icon::show('trash', ['title' => 'delete'])
Icon::show('calendar', ['class'=>'fa-2x'])

You can find more examples on this demopage: http://demos.krajee.com/icons