In this tutorial you will learn about the Laravel Create Custom Facade Class Tutorial and its application with practical example.
In this Laravel Create Custom Facade Class Tutorial I will show you how to Create Custom Facade in laravel 8. In this tutorial you will learn to create Custom Facade in laravel. In this article I will guide you through the process to create create Custom Facade Class in laravel 8.
What is Facade Class in Laravel?
The facade is a class that means to access an object from the container. Any facade you create basically extends the base Illuminate\Support\Facades\Facade class. The Laravel framework provide default facade like App, Cookie, Route, Redirect, Crypt etc.
Laravel Create Custom Facade Class Tutorial
In this step by step tutorial I will demonstrate you with example on how to create and use Custom Facade in laravel 8 application. Please follow the instruction given below:
- Step 1 – Create a PHP class file
- Step 2 – Bind that class to Service Provider
- Step 3 – Register that Service Provider in
Config\app.php
- Step 4 – Create a class that extends Illuminate\Support\Facades\Facade
- Step 5 – Register your facade in
Config\app.php
as aliases
Step 1: Install Laravel 8
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Create Class File
In the first step we will create a “DemoFacade” directory in our project’s app directory and create file DemoDateClass.php(app/DemoFacade/DemoDateClass.php). Then put the following code in DemoDateClass.php file.
app/DemoFacade/DemoDateClass.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php namespace App\DemoFacade; use Carbon\Carbon; class DemoDateClass{ /** * 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
Now we will create a facade class app/DemoFacade/DemoDateClassFacade.php, In this class we have to extend “Illuminate\Support\Facades\Facade” class:
app/DemoFacade/DemoDateClassFacade.php
1 2 3 4 5 6 7 8 |
<?php namespace App\DemoFacade; use Illuminate\Support\Facades\Facade; class DemoDateClassFacade extends Facade{ protected static function getFacadeAccessor() { return 'demodateclass'; } } |
Step 4: Create ServiceProvider Class
In this step we will create a service provider to bind class. Use the following command to create a service provider:
1 |
php artisan make:provider DemoFacadeServiceProvider |
Now, we have DemoFacadeServiceProvider.php in providers(app/Providers) directory. Open app/Providers/DemoFacadeServiceProvider.php and put the following code:
app/Providers/DemoFacadeServiceProvider.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use App\DemoFacade\DemoDateClass; class DemoFacadeServiceProvider extends ServiceProvider { /** * Register services. * * @return void */ public function register() { } /** * Bootstrap services. * * @return void */ public function boot() { $this->app->bind('demodateclass',function(){ return new DemoDateClass(); }); } } |
Finally you need to register service provider in Config\app.php as following:
config/aap.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php return [ ... 'providers' => [ .... App\Providers\DemoFacadeServiceProvider::class, ], 'aliases' => [ ... 'DemoDateClass'=> App\DemoFacade\DemoDateClassFacade::class ] |
Now we need to run composer dump-autoload command as bellow:
1 |
composer dump-autoload |
Step 5: Use Facade Class
Now we can use facade class as following:
1 2 3 4 5 6 7 8 9 10 11 |
Route::get('it-date-class', function(){ $getYMD = DemoDateClass::dateFormatYMD('10/21/2021'); print_r($getYMD); $getMDY = DemoDateClass::dateFormatMDY('2021-11-22'); print_r($getMDY); }); |
Output:-
1 2 |
2021-10-21 11/22/2021 |