Laravel Create Custom Facade Class Tutorial

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

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

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

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:

Now, we have DemoFacadeServiceProvider.php in providers(app/Providers) directory. Open app/Providers/DemoFacadeServiceProvider.php and put the following code:

app/Providers/DemoFacadeServiceProvider.php

Finally you need to register service provider in Config\app.php as following:

config/aap.php

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

Step 5: Use Facade Class

Now we can use facade class as following:

Output:-

In this tutorial we have learn about the Laravel Create Custom Facade Class Tutorial and its application with practical example. I hope you will like this tutorial.