Laravel Create Custom Facade Class Example

By Hardik Savani April 16, 2024 Category : Laravel

Hi Artisan,

This is a short guide on how to create custom facade in laravel. if you have question about laravel create custom facade then i will give simple example with solution. if you want to see example of laravel create your own facade then you are a right place. i would like to share with you create custom facade laravel. follow bellow step for laravel make facade class.

you can easily create custom facade in your laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version project.

What is Facade Class in Laravel?

In a Laravel application, a facade is a class that provides access to an object from the container. The machinery that makes this work is in the Facade class. Laravel's facades, and any custom facades you create, will extend the base Illuminate\Support\Facades\Facade class.

Laravel provide default facade like Route, Redirect, Cookie, App, Crypt etc. same facade if you want to create for your own application then i will help you step by step to creating custom facade in laravel application. just follow bellow steps:

Step 1: Install Laravel

first of all we need to get fresh Laravel 8 version application using bellow command, So open your terminal OR command prompt and run bellow command:

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

Step 2: Create Class File

In Following step, first you should create "ItSolution" directory in app folder and create file ItDateClass.php(app/ItSolution/ItDateClass.php).now copy the following code in your ItDateClass.php file.

app/ItSolution/ItDateClass.php

<?php

namespace App\ItSolution;

use Carbon\Carbon;

class ItDateClass {

/**

* 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 Facade Class

In this step create another class app/ItSolution/ItDateClassFacade.php, In this class you have to extend "Illuminate\Support\Facades\Facade" class, copy following link:

app/ItSolution/ItDateClassFacade.php

<?php

namespace App\ItSolution;

use Illuminate\Support\Facades\Facade;

class ItDateClassFacade extends Facade{

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

}

Step 4: Create ServiceProvider Class

In this step you should create service provider for bind class, so fire your terminal or cmd this following command:

php artisan make:provider ItSolutionServiceProvider

Ok, now you can find new file ItSolutionServiceProvider.php in your providers(app/Providers) directory.So, Basically open app/Providers/ItSolutionServiceProvider.php and copy the following code:

app/Providers/ItSolutionServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use App\ItSolution\ItDateClass;

class ItSolutionServiceProvider extends ServiceProvider

{

/**

* Register services.

*

* @return void

*/

public function register()

{

}

/**

* Bootstrap services.

*

* @return void

*/

public function boot()

{

$this->app->bind('itdateclass',function(){

return new ItDateClass();

});

}

}

Simple Register your service provider in Config\app.php like this way:

config/aap.php

<?php

return [

...

'providers' => [

....

App\Providers\ItSolutionServiceProvider::class,

],

'aliases' => [

...

'ItDateClass'=> App\ItSolution\ItDateClassFacade::class

]

Now we need to run composer dump-autoload command as bellow:

composer dump-autoload

Step 5: Use Facade Class

let's use facade class as bellow:

Route::get('it-date-class', function(){

$getYMD = ItDateClass::dateFormatYMD('10/21/2021');

print_r($getYMD);

$getMDY = ItDateClass::dateFormatMDY('2021-11-22');

print_r($getMDY);

});

Output:

2021-10-21

11/22/2021

now you can check your own.

I hope it can help you...

Tags :
Shares