Laravel Create Custom Facade Class Example
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 8 Model Observers Tutorial Example
- Laravel Model Events Tutorial
- Laravel 8 Create Custom Helper Functions Tutorial
- Laravel Form Validation Request Class Example
- Laravel 6 Form class not found - Solution
- Laravel Multi Language Translation using lang() Helper Tutorial
- Laravel Custom Helper Facade Class Example
- Laravel Redirect to URL using redirect() Helper
- Laravel Localization(trans Helper) Tutorial Example
- How to create custom facade in laravel 5.2?
- How to Create Custom Helper Function in Laravel?