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

How to use if else condition in Laravel 8 blade file ?

December 17, 2021January 18, 2022

In this tutorial i will show you to use if else, if and else if in Laravel blade. Laravel use blade engine to render the template and blade have rich features to use conditional statements in file. So here i will show you to use @if, @elseif and @else in…

Read More
Php How to Share URL on social media using package in Laravel 98765

How to Share URL on social media using package in Laravel 9/8/7/6/5 ?

March 15, 2022October 4, 2022

In this tutorial i will show you to Share URL on social media using package in Laravel. Laravel provides several packages to make the task easy, In the same way we can add third party package to share the URLs on multiple social media using the package jorenvanhocht/laravel-share. This package…

Read More
Php How to Search JSON Data in Database Laravel

How to Search JSON Data in Database Laravel ?

August 2, 2022March 16, 2024

JSON Data in database is used to store the informational data. JSON can be easily parse and stringify so it can be stored easily in database. In this post i will show you to search JSON data in database laravel. Laravel by default configured with MySQL and we will store…

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

  • 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 Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version