create custom facade in laravel

While creating a Laravel application, you will see facades many times like route facades, cache facades, Auth, and many more.

What are facades?

Facades provide a static interface to classes that are available in the application’s service container. Laravel ships with many facades which provide access to almost all of Laravel’s features.

In simple terms, A facade is a class wrapping a complex library to provide a simpler and more readable interface to it.

You can define a facade simply by using the getFacadeAccessor method for a facade class.

First of all, we need to create a new Laravel application or you can use an existing application. Here, we have divided the process into a few steps so you can understand it easily:

  1. Creating PHP Class File
  2. Binding Class file with Service Provider
  3. Register Service Provider to Application
  4. Create another class that extends Illuminate\Support\Facades\Facade
  5. Register Our Facade in the Application
  6. Testing our newly Created Custom Facade

Creating PHP Class File

First of all, We need to create a simple PHP class file that has our functions. In this example we have created app\Codewolfy\Codewolfy.php:

<?php
namespace App\Codewolfy;

class Codewolfy
{
    public function welcome()
    {
        echo "Welcome to Codewolfy.com";
    }
}

Binding Class file with Service Provider

After creating a class file, we need to create a service provider. In this example, we will create CodewolfyServiceProvider using the below command :

php artisan make:provider CodewolfyServiceProvider

It will create a new service provided at the App\Providers directory. Now we have to bind the method to a newly created service provider. After binding our service provider will look like the below:

<?php

namespace App\Providers;

use App\Codewolfy\Codewolfy; //import
use Illuminate\Support\ServiceProvider;

class CodewolfyServiceProvider extends ServiceProvider
{
    public function register()
    {
        //Binding our class to service provider
        $this->app->bind('codewolfy',function(){
            return new Codewolfy();
        });
    }

    public function boot()
    {
        //
    }
}

Register Service Provider to Application

After modification of the service provider, we need to add to our application so we can access it. To register the service provider to the application, we need to add the below line to the config/app.php file:

/*
* Application Service Providers...
*/
...
...
Register Service Provider to Application

Creating another class that extends Facades

We are going to create another PHP class file at app/Codewolfy/CustomFacade.php, you can rename or move this file to any location in the app directory. This class will only extend Facades and has only the method getFacadeAccessor.

<?php

namespace App\Codewolfy;

use Illuminate\Support\Facades\Facade;

class Customfacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'codewolfy';
    }
}

Register our Facade in the Application

At last, we have to register the CustomFacade class to our application. For that open config/app.php and add the below line under aliases.

'Codewolfy'   =>  App\Codewolfy\Customfacade::class

Testing our newly Created Custom Facade

Now you have created a custom facade for our application. let’s create a new route that calls a welcome method from our facade. Open your route file and add the following lines to it :

Route::get('/welcome', function() {
    Codewolfy::welcome();
});

For testing open your browser with this route and you will welcome the message to output.

I hope this blog will help you to implement a custom facade into your application and if you have any queries then please comment below.