Spread the love

In Laravel there several facades to use like DB, URL, Validator ,Request etc. Facades are used to use the class methods statically using the application service container. In this article we will learn to create our own facades with service container.

Let’s start with step by step

Step 1 : Create a class

For a better a structure and understandable code we are going to create a folder facade in app folder and then we create a file CustomFacade.php

<?php
namespace App\Facades;
class DateHelper
{
    public function hello()
    {
        echo "Hello, I am Readerstacks";
    }
}

Step 2 : Create a service provider

Now to register our new facade we need to register it in Laravel service container so run below command to generate the service provider

php artisan make:provider DateHelperServiceProvider

and then add bind our facade in register function

<?php

namespace App\Providers;

use App\Facades\DateHelper;
use Illuminate\Support\ServiceProvider;

class DateHelperServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('datehelper',function(){
            return new DateHelper();
        });
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

Here we called bind method which accepts name and anonymous function to resolve the dependency.

Step 3 : Register the new service provider

Service Providers not come into the effect until it’s register to config/app.php providers array thus let’s register the our new service provider as below

 'providers' => [
   ......
   ......
        App\Providers\DateHelperServiceProvider::class

    ],

Step 4 : Create a facade class

We have created a class, service provider and registered it to app.php now we need to create a facade to access the method of DateHelper class statically so create a facade as below in same location app\Facades\DateHelperFacade.php and it extends class Illuminate\Support\Facades\Facade . we need to create getFacadeAccessor method which return the name of facade same as we passed in bind method in service provider.

<?php
namespace App\Facades;


use Illuminate\Support\Facades\Facade;
class DateHelperFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'datehelper'; // same as bind method in service provider
    }
}

Now we will be able to access our new facade as below

<?php

use Illuminate\Support\Facades\Route;
 

Route::get('/facade',function(){
     return App\Facades\DateHelperFacade::hello();
});

What if we wanted to access it using alias then we need to register it in aliases.

Step 5 : Create a alias

So, if you wanted to access without full namespace of the class then we nee to define it in config/app.php aliases array as below

'aliases' => [

         ........
        'Date' => App\Facades\DateHelperFacade::class
        
    ],

Now check our implementation by creating a route

<?php

use Illuminate\Support\Facades\Route;
 

Route::get('/facade',function(){

    return Date::hello();

}); 
Laravel Custom Facade example

Leave a Reply