Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel Custom Facade with example

How to create a custom facade with example in Laravel 8 ?

Aman Jain, December 22, 2021January 10, 2022

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();

}); 
Screenshot 2021 12 22 at 8.19.59 AM
Laravel Custom Facade example

Related

Php Laravel facadelaravelphp

Post navigation

Previous post
Next post

Related Posts

Php Compare date in where query laravel

How to compare date in where query laravel eloquent

February 5, 2022November 14, 2023

Laravel use carbon library to format and show the date and to compare date in where query laravel or db builder it uses carbon internally so compare date in where query in laravel is almost same as executing where using other data type but if we wanted to go more…

Read More
Laravel 3 Ways to Remove public from URL in Laravel

3 Ways to Remove public from URL in Laravel

August 22, 2021November 8, 2023

Laravel comes with default server for local environment and but if run the application in apache then we need to remove the public from URL because if we serve the application through the Apache then we need to call the URLs from public directory. It can be multiple way to…

Read More
Laravel folder paths using helpers in laravel

How to get folder paths using helpers in laravel?

August 27, 2021February 22, 2024

Laravel has helpers to get folder paths using helpers in laravel and by which we can easily get root folder, public folder, assets folder, storage folder, app folder etc. Laravel has helpers to get the path of root folder, public folder, assets folder, storage folder, app folder. To get the…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version