How to Create Custom Facade in Laravel 10

Websolutionstuff | Sep-22-2023 | Categories : Laravel PHP

Laravel, one of the most popular PHP frameworks, provides a powerful feature known as facades. Facades allow you to access Laravel services and classes in a clean and expressive manner. While Laravel provides a wide range of built-in facades for common tasks.

You may encounter scenarios where you want to create your custom facade to encapsulate your own functionality. In this tutorial, we'll explore how to create a custom facade in Laravel 10, enabling you to abstract complex operations into a simple and elegant interface.

Facades provide a "static" interface to classes that are available in the application's service container.

All of Laravel's facades are defined in the Illuminate\Support\Facades namespace. So, we can easily access a facade like so:

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
 
Route::get('/cache', function () {
    return Cache::get('key');
});

 

Step 1: Create a New Laravel Project

If you haven't already, create a new Laravel project or use an existing one. You can do this by running the following command.

composer create-project --prefer-dist laravel/laravel project-name

 

Step 2: Create Class File

In the following step, first, you should create a "WSS" directory in the "app" folder and create a file named "WSSDateClass.php" (app/WSS/WSSDateClass.php).

Now, copy the following code into your "WSSDateClass.php" file.

<?php

namespace App\WSS;
use Carbon\Carbon;

class WSSDateClass {

    /**
     * Write code on Method
     *
     * @return response()
     */

    public function dateFormatYMD($date){

        return Carbon::createFromFormat('m/d/Y', $date)->format('Y-m-d');

    }

    /**
     * Write code on Method
     *
     * @return response()
     */

    public function dateFormatMDY($date){

        return Carbon::createFromFormat('Y-m-d', $date)->format('m/d/Y');

    }

}

 

 

Step 3: Create a Facade Class

Open the WSSDateClassFacade.php file and define your custom facade.

app/WSS/WSSDateClassFacade.php

<?php

namespace App\WSS;
use Illuminate\Support\Facades\Facade;

class WSSDateClassFacade extends Facade{

    protected static function getFacadeAccessor() { 
       return 'wssdateclass'; 
    }
}

 

Step 4: Create ServiceProvider Class

A custom facade typically requires a custom service provider. Run the following command to generate a service provider.

php artisan make:provider WSSServiceProvider

This will create a WSSServiceProvider.php file in the app/Providers directory.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\WSS\WSSDateClass;  

class WSSServiceProvider extends ServiceProvider
{

    /**
     * Register services.
     *
     * @return void
     */

    public function register()
    {     
        $this->app->bind('wssdateclass',function(){
            return new WSSDateClass();
        });
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */

    public function boot()
    {
        
    }
}

Then, we register the service provider in the app.php file.

config/app.php

<?php

return [
    ...
    'providers' => [
        ....

        App\Providers\WSSServiceProvider::class,
    ],


    'aliases' => [
        ...
        'WSSClass'=> App\WSS\WSSDateClassFacade::class
    ]
]

Now, we need to run the composer dump-autoload command as below.

composer dump-autoload

 

Step 5: Use Facade Class

let's use the facade class as below.

Route::get('get-date-class', function(){
    $getYMD = WSSDateClass::dateFormatYMD('09/22/2023');
    print_r($getYMD);

    $getMDY = WSSDateClass::dateFormatMDY('2023-09-22');
    print_r($getMDY);
});

Output:

2023-09-22

09/22/2023

 

Conclusion

Creating a custom facade in Laravel 10 is a powerful way to encapsulate complex functionality and provide a clean and expressive interface for your application. With this tutorial, you've learned how to create and use custom facades to improve the maintainability and readability of your Laravel code.

 


You might also like:

Recommended Post
Featured Post
Laravel 8 Datatables Filter with Dropdown
Laravel 8 Datatables Filter wi...

In this example we will see laravel 8 datatables filter with dropdown, Here we will add datatables custom...

Read More

Jun-16-2021

Laravel 8 Ajax CRUD With Yajra Datatable
Laravel 8 Ajax CRUD With Yajra...

In this tutorial, we will see laravel 8 ajax crud with yajra datatable. I will show you how to create ajax crud ope...

Read More

Jan-05-2022

Jquery Append And Prepend Example
Jquery Append And Prepend Exam...

In this article I will give you example of jquery append() and prepend() method. The append() method inserts t...

Read More

Dec-06-2021

How To Get Last 15 Days Records In MySQL
How To Get Last 15 Days Record...

In this tutorial, we will see how to get the last 15 days records in MySQL PHP.  In PHP, you can use INTERVAL...

Read More

Feb-09-2022