create custom facade in laravel

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

What is facades?

Facades provide a static interface to class 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 getFacadeAccessor method for a facade class.

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

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

Creating PHP Class File

First of all, We need to create simple PHP class file which 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 class file, we need to create service provider. In this example, we will create CodewolfyServiceProvider using below command :

    
php artisan make:provider CodewolfyServiceProvider
    

It will create new service provide at App\Providers directory. Now we have to binding method to newly created service provider. After binding our service provider will look like 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 service provider we need to add into our application so we can access it. For register service provider to application we need to add below line to 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 at any location in app directory. This class will only extends Facades and has only 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 Application

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

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

Testing our newly Created Custom Facade

Now you have created custom facade into our application. let's create new route that call welcome method from our facade. Open your route file and add following lines to it :

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

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

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


Share your thoughts

Ask anything about this examples