Menu

Creating a Custom Facade in Laravel 6.0

January 27, 2020 - Backend Development
Creating a Custom Facade in Laravel 6.0

In this article, we will discuss on creating a custom facade in Laravel 6.0 application.

Why use a facade?

A facade can help reduce code duplicity and make it easier to use, understand and test throughout an application. Usually facades are used to reduce dependencies of outside code on the inner workings of a library.

Thus, by creating a custom facade you will be able to create your own custom library to allow more flexibility in developing your application.

Implementing a Custom Facade in Laravel 6.0

In order to create a facade in Laravel 6, we will need to follow the following steps –

Step 1: Adding a PHP File

Create a folder named “Services” inside the app folder (i.e. app/Services) of your Laravel Application.

Inside this folder create a PHP File named “CustomServices“. This is the PHP Library file that our Facade is going to expose. For the purpose of the example, we have created a simple function “getVerificationCode” which returns a randomly generated verification code.

<?php

namespace App\Services;

class CustomServices{
    
  /*=============================================
  * Get Verification Code for User Registration
  *
  * @param optional (random number minimum and maximum range)
  * @return int (between $min/$max)
  #=============================================*/
  public function getVerificationCode($min = 10000, $max=99999){
    return rand($min, $max);
  }

}

Step 2: Registering the PHP File to the Service Provider

Now register the CustomServices class inside a service provider. You can create a new custom provider or simply add it to the register function in app/Providers/AppServiceProvider.php.

  public function register()
  {
    //Registering our CustomServices File as a Service Provider
    $this->app->singleton('CustomServicesAlias', function ($app) {
      return new \App\Services\CustomServices;
    });
   }

Step 3: Creating the Facade

Now, we will Create a folder named “Facade” inside the app folder (i.e. app/Facades)

Now create a class that extends Illuminate\Support\Facades\Facade class under app/Facades. For the purpose of the example, let us name the class as “CustomServicesFacade“.

CustomServicesFacade” extends the base Facade class and defines the method getFacadeAccessor(). This method’s job is to return the name of a service container binding.

<?php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class CustomServicesFacade extends Facade
{
  
  protected static function getFacadeAccessor()
  {
    //Note - Returning the name of Service Container Binding
    return 'CustomServicesAlias';
  }
}

Step 4: Registering the Alias in config/app.php

Register the alias of CustomServicesFacade in config/app.php

Now open app/config.php file and add the CustomServiceFaade to the Class Alias array as shown below –

   'CustomServicesFacade' => App\Facades\CustomServicesFacade::class

Using the Facade

Now you are ready to use the Custom Facade throughout your Laravel 6 application. For the purpose of the demo, we have added a snippet of a controller using the getVerificationCode method via our CustomServicesFacade –

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use CustomServicesFacade; //Importing our Facade Alias

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function getCode(Request $request, $id){
       $verificationCode=CustomServicesFacade::getVerificationCode();
       return $verificationCode;
    }
}

That is it in creating a custom facade in Laravel 6.0 application! Do let me know if you have any questions! Thanks

2 thoughts on “Creating a Custom Facade in Laravel 6.0

blog

You can certainly see your enthusiasm in the work you
write. The sector hopes for more passionate writers like you who aren’t afraid to say how they believe.
Always go after your heart.

Reply
    Fahim Hossain, Director & CEO at ARITS Limited
    Fahim Hossain

    Thank you and I hope the article was useful for you. We will keep trying our best!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *