In this article, we'll see how to create a custom facade in laravel 10. In laravel 10, create custom facades and use them. Laravel, one of the most popular PHP frameworks, provides a powerful feature called facades.
Facades allow you to access Laravel services and classes cleanly and expressively. While Laravel provides a wide range of built-in facades for common tasks.
Facades provide a "static" interface to classes 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 this:
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
Route::get('/cache', function () {
return Cache::get('key');
});
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
In the following step, first, you should create a "VD" directory in the "app" folder and create a file named "VDDateClass.php" (app/VD/VDDateClass.php).
Now, copy the following code into your "VDDateClass.php" file.
<?php
namespace App\VD;
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');
}
}
Open the VDDateClassFacade.php file and define your custom facade.
app/VD/VDDateClassFacade.php
<?php
namespace App\VD;
use Illuminate\Support\Facades\Facade;
class VDDateClassFacade extends Facade{
protected static function getFacadeAccessor() {
return 'vddateclass';
}
}
A custom facade typically requires a custom service provider. Run the following command to generate a service provider.
php artisan make:provider VDServiceProvider
This will create a VDServiceProvider.php
file in the app/Providers
directory.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\VD\VDDateClass;
class VDServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->bind('vddateclass',function(){
return new VDDateClass();
});
}
/**
* 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\VDServiceProvider::class,
],
'aliases' => [
...
'VDClass'=> App\VD\VDDateClassFacade::class
]
]
Now, we need to run the composer dump-autoload command as below.
composer dump-autoload
let's use the facade class as below.
Route::get('get-date-class', function(){
$getYMD = VDDateClass::dateFormatYMD('09/22/2023');
print_r($getYMD);
$getMDY = VDDateClass::dateFormatMDY('2023-09-22');
print_r($getMDY);
});
Output:
2023-09-22
09/22/2023
You might also like:
- Read Also: How to Send SMS using Twilio in Laravel 10
- Read Also: Laravel 10 With Vue JS CRUD Operation
- Read Also: How to Send Bulk Mail using Queue in Laravel 10
- Read Also: Laravel 10 Setup Cron Job Task Scheduler