Blog Detail

13

May
Fetch Favicons From Websites in Laravel With Favicon Fetcher cover image

arrow_back Fetch Favicons From Websites in Laravel With Favicon Fetcher

Favicon Fetcher is an awesome Laravel package that can be used for fetching favicons from websites. The package has been developed and tested to work with the following minimum requirements:

  • PHP 8.0
  • Laravel 8.0

Installation

You can install the package via Composer:

composer require ashallendesign/favicon-fetcher

Publish the Config

You can then publish the package’s config file by using the following command:

php artisan vendor:publish --provider="AshAllenDesign\FaviconFetcher\FaviconFetcherProvider"

Usage

Fetching Favicons

Now that you have the package installed, you can start fetching the favicons from different websites.

Using the fetch Method

To fetch a favicon from a website, you can use the fetch method which will return an instance of AshAllenDesign\FaviconFetcher\Favicon:

use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$favicon = Favicon::fetch('https://ashallendesign.co.uk');

Using the fetchOr Method

If you’d like to provide a default value to be used if a favicon cannot be found, you can use the fetchOr method.

For example, if you wanted to use a default icon (https://example.com/favicon.ico) if a favicon could not be found, your code could look something like this:

use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$favicon = Favicon::fetchOr('https://ashallendesign.co.uk', 'https://example.com/favicon.ico');

This method also accepts a Closure as the second argument if you’d prefer to run some custom logic. The URL field passed as the first argument to the fetchOr method is available to use in the closure. For example, to use a closure, your code could look something like this:

use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$favicon = Favicon::fetchOr('https://ashallendesign.co.uk', function ($url) {
    // Run extra logic here...

    return 'https://example.com/favicon.ico';
});

Drivers

Favicon Fetcher provides the functionality to use different drivers for retrieving favicons from websites.

Available Drivers

By default, Favicon Fetcher ships with 4 drivers out-the-box: http, google-shared-stuff, favicon-kit, unavatar.

The HTTP driver fetches favicons by attempting to parse “icon” and “shortcut icon” link elements from the returned HTML of a webpage. If it can’t find one, it will attempt to guess the URL of the favicon based on common defaults.

The google-shared-stuff driver fetches favicons using the Google Shared Stuff API.

The favicon-kit driver fetches favicons using the Favicon Kit API.

The unavatar driver fetches favicons using the Unavatar API.

Choosing a Driver

You can select which driver to use by default by changing the default field in the favicon-fetcher config file after you’ve published it. The package originally ships with the HTTP driver enabled as the default driver.

For example, if you wanted to change your default driver to favicon-kit, you could update your favicon-fetcher config like so:

return [

    // ...
        
    'default' => 'favicon-kit',
            
    // ...

]

If you’d like to set the driver on-the-fly, you can do so by using the driver method on the Favicon facade. For example, if you wanted to use the google-shared-stuff driver, you could do so like this:

use AshAllenDesign\FaviconFetcher\Facades\Favicon;

$favicon = Favicon::driver('google-shared-stuff')->fetch('https://ashallendesign.co.uk');

This package has a lot more features with code examples. If you want to dig more you can visit its complete documentation and Source Code on Github.

Published at : 13-05-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project