How to Create and Use Custom Facade in Laravel

tutspack laravel
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Hello Artisans,

Laravel custom Facades provide a static interface to a class that gives access to an object from the service container, let’s look at Laravel’s Custom Facades. So we can call our function by declaring it static. Cause Facades provide a static interface to a class.

In this tutorial, I am going to show step-by-step laravel custom facades, and also we will see how can we use it in our laravel application.

Sometimes we use over and again code and repeatedly use function at that time we can create a custom facade for our project, that way you can create functions and use it

In this tutorial will create a custom facade in Laravel by following below few short steps. So let’s start our laravel custom facades tutorial.

Step 1: Create Test.php Class

In the Following step, first, you should create the “Repositories” directory in the app folder and create the file Check.php. Now copy the following code in your Test.php file.

app/Repositories/Test.php

namespace App\Repositories;

class Test
{
    public function greetings()
    {
     return "Hello Artisans";
    }
}

Step 2: Create Our Own Custom ServiceProvider

In this step you will create a custom service provider for bind class, so open your terminal or cmd run the following command:

php artisan make:provider CustomFacadesProvider

Well, now you can find new file CustomFacadesProvider.php in your providers(app/Providers) directory and paste this following code.

app/Providers/CustomFacadesProvider.php

amespace App\Providers;

use Illuminate\Support\ServiceProvider;

class CustomFacadesProvider extends ServiceProvider
{
   
    public function register()
    {  
        app()->bind('test', function(){  //Keep in mind this "test" must be return from facades accessor
            return new \App\Repositories\Test;
        });
    }

    public function boot()
    {
    }

Step 3: Create Facade Class

In this step, You have to create another class in our Repositories directory called TestFacades.php In this class you have to extend the “Illuminate\Support\Facades\Facade” class, copy and paste this following code.

app/Repositories/TestFacades.php

namespace App\Repositories;

use Illuminate\Support\Facades\Facade;

class TestFacades extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'test';
    }
}

Step 4: Register Service Provider

Now time to register our provider and facades in the following path

config/app.php

// In providers array
App\Providers\CustomFacadesProvider::class,

And also add aliase for facade in this file like this way :

'CustomFacade' => App\Repositories\TestFacades::class,

Step 5 :  Create Route

Now we can access our Test.php class method with scope resolution via CustomFacade facades aliased. Now just check it after visiting this route URL.

Route::get('test', function(){

    CustomFacade::greetings();

});
 

Now open your project, using php artisan serve. After visiting this route url, you should see the following output. Custom Facades”Hello Artisans”

Hope it can help you.


Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  

About Anisur Rahman Shahin

Hello. My name is Shahin. I'm a tech enthusiast guy. Personally, I’m Optimistic and always in hurry kinda person. I'm a freelance web developer. I am working at Bangla Kings Software as Junior Laravel Developer. My Portfolio website: https://devshahin.com/

View all posts by Anisur Rahman Shahin →

Leave a Reply

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