Laravel logo with a facade panel over gears, illustrating creating a custom facade in Laravel
Back to Blog
Backend Development
January 27, 2020
Fahim Hossain

Creating a Custom Facade in Laravel 6.0

How to create a custom facade in Laravel, why a facade reduces duplication, and what it does to how easily you can test the code behind it.

Laravel logo with a facade panel over gears, illustrating creating a custom facade in Laravel

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

Building something bigger than a code snippet? ARITS does full-lifecycle custom software engineering.

Need extra engineers, or a team to run the build?

Tell us the shape of the work and your deadline. You will get a straight read on what it takes and what it will cost, from a team that has shipped 400+ projects out of Dhaka and London.

Cookies

We use cookies to see how people find this site and to measure our ads. You can turn those off — everything here still works. Privacy policy

Necessary

Keeps the site working and remembers this choice.

Always on

Analytics

Anonymous stats on which pages get read, so we know what to write next.

Marketing

Lets us measure which ads brought someone here.