Back to Blog
Backend Development
November 4, 2025
Fahim Hossain

Creating a Custom Facade in Laravel 6.0

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.

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

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript