Creating a Custom Facade in Laravel 6.0

January 27, 2020
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.


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.


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 –


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){
       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

Fahim Hossain

Fahim Hossain

With the booming pace of the tech industry, there is a lot to grasp, learn and master. My passion is to work with different architectural design patterns and developing high performance software solutions using the latest technology stacks. I am the founder of ARITS Limited and am currently serving as its Chief Executive Officer

2 on “Creating a Custom Facade in Laravel 6.0


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.

    Fahim Hossain
    Fahim Hossain

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


Leave a Reply

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

Advancing Through Technology
Ground Floor • House 375 • Lane 6W
Baridhara DOHS • Dhaka • 1206 • Bangladesh
Email :
Call: ‭+880 19 0519 9835
Automation • Robotics • Information • Technology • Solutions
© 2019 ARITS Limited • Advancing Through Technology
All rights reserved.