Fork me on GitHub

Font Awesome for PHP

A composer ready package designed to integrate the fantastic Font Awesome icon set into your PHP projects through an easy to use interface.

Created with Laravel in mind, a ServiceProvider and Facade have been included as well.

Don't worry though, the library will work in any PHP application, via composer or manually.

 

Installation

With Composer

First, add the package to your main composer.json file:

"khill/fontawesomephp" : "~2.0"

Next, run composer from the command line to download and install:

composer update

Then, if you are using Laravel, add the ServiceProvider to the service providers array in the app.php file:

Skip this step if you are not using Laravel

'Khill\Fontawesome\FontAwesomeServiceProvider'

Last, add the link in your view's page header to the FontAwesome CSS file, provided by BootstrapCDN:

FontAwesome::css() // Or FA::css() if you want to use the alias

Manually

First, Download the zip file from above and extract the src folder into your project. Then, include the main class and you're good to go!

include("[UNZIPPED_ARCHIVE]/src/FontAwesome.php");

Next, Copy the assets (css & fonts) from the library's public folder to your project's assets folder to include them manually:

public/css/font-awesome.min.css
public/fonts/fontawesome-webfont.eot
public/fonts/fontawesome-webfont.svg
public/fonts/fontawesome-webfont.ttf
public/fonts/fontawesome-webfont.wof
public/fonts/FontAwesome.otf

Last, add the link to the Font Awesome CSS file in your page header:

<link href="[ASSET_DIR]/css/font-awesome.min.css" rel="stylesheet" />
 

Usage

The Laravel Way

Since we love Laravel, and making life easy, we took care of aliasing the library with a facade, so calls are simple:

echo FA::icon('star');

will output the icon html for you

<i class="fa fa-star"></i>

Generic PHP

If you are using this library in a different framework, then create a new instance of FontAwesome, and call from the object:

$fa = new FontAwesome;
echo $fa->icon('star');

will output the icon html for you

<i class="fa fa-star"></i>
 

Examples

All of the methods of the API correspond with how Font Awesome is used, to make FontAwesomePHP intuitive and easy to use. We've also thrown in a few extra features.


Inline Icons:

Add icons anywhere with this simple syntax.

icon('home');
icon($iconLabel);

param $iconLabel string The name of the icon to display (omit the prefix "fa-")

returns string Icon HTML


Sizing Icons:

Increase an icon's size with the following methods.

lg('rocket');
x2('rocket');
x3('rocket');
x4('rocket');
x5('rocket');
lg($iconLabel);
x2($iconLabel);
x3($iconLabel);
x4($iconLabel);
x5($iconLabel);

param $iconLabel string The name of the icon to display (omit the prefix "fa-")

returns string Icon HTML


Fixed Width Icons:

Set icons to have a fixed width, perfect for menus or buttons.

fixedWidth($iconLabel);

param $iconLabel string The name of the icon to display (omit the prefix "fa-")

returns string Icon HTML

Example: (Laravel alias within blade template shown)

<ul class="nav nav-pills nav-stacked">
    <li class="active"><a href="#">{{ FA::fixedWidth('home') }} Home</a></li>
    <li><a href="#">{{ FA::fixedWidth('flask') }} Science</a></li>
    <li><a href="#">{{ FA::fixedWidth('group') }} Connect</a></li>
    <li><a href="#">{{ FA::fixedWidth('upload') }} Upload</a></li>
</ul>

List Icons:

Easily replace default bullets in unordered lists, or assign multiple icons per line.

  • list item one
  • list item two
  • list item three

  • This is my first item
  • This is my second item
  • This is my third item
ul('square')->li('list item one')
            ->li('list item two')
            ->li('list item three')

ul() param $defaultIcon string The name of the icon to use in list (omit the prefix "fa-")

returns FontAwesome

li() param $listItem string|array Items to add to list, either line by line, or array of lines.

returns string Icon HTML

ul(array(
    'magic' => 'This is my first item',
    'music' => 'This is my second item',
    'road'  => 'This is my third item'
));

ul() param $completeList string Associative array of icon => line pairs

returns string Icon HTML


Bordered & Pulled Icons:

border('github')
left('camera')
right('cloud')
border('github');
left('camera');
right('cloud');

param $iconLabel string The name of the icon to display (omit the prefix "fa-")

returns string Icon HTML


Spinning Icons:

Animate any icon to spin with this method.

spin('question-circle')
spin('refresh')
spin('question-circle')
spin('refresh')

param $iconLabel string The name of the icon to display (omit the prefix "fa-")

returns string Icon HTML


Rotated & Flipped Icons:

Any icon can be flipped and rotated with these following methods.

rotate90('pencil')
rotate180('leaf')
rotate270('gamepad')
flipHorizontal('signal')
flipVertical('tags')
rotate90('pencil');
rotate180('leaf');
rotate270('gamepad');
flipHorizontal('signal');
flipVertical('tags');

param $iconLabel string The name of the icon to display (omit the prefix "fa-")

returns string Icon HTML


Stacked Icons:

Stacking icons is simple, just chain the methods together using the following syntax.


stack('ban')->on('comments')

stack('square-o')->on('gavel')->lg()

stack('circle-o')->on('twitter')->x3()
stack('ban')->on('comments');
You can also chain on size modification methods
stack('square-o')->on('gavel')->lg();
stack('circle-o')->on('twitter')->x3();

param $iconLabel string The name of the icon to display (omit the prefix "fa-")

returns string Icon HTML


Chaining

The methods can be chained together to create dynamic icons


FA::x3('shopping-cart')->flipVertical()

FA::rotate270('cutlery')->x3()

FA::icon('truck')->x4()->inverse()

FA::right('hospital')->border()->x2()
x3('shopping-cart')->flipVertical();
rotate270('cutlery')->x3();
icon('truck')->x4()->inverse();
right('hospital')->border()->x2();

returns FontAwesome All of the methods return the FontAwesome object so that they can be chained together. The magic method __toString() will output the HTML once you echo or print the object.


 

Collection

Icons can be stored into a collection to later be recalled from within a view.

x3('cog')->store('savedIcon1'); //Store Icons
echo collection('savedIcon1'); //Retrieve within template or HTML