In this post, I will cover what is facades in Laravel, how to create your own custom facades and use them in Laravel applications.

Probably, facades are one of the discussed topics in the laravel community.

According to Dictionary.com

The word ‘facade’ refers to a “superficial appearance or illusion of something.” In architecture, the term refers to the front of a building. Any side of a building facing a public way or space and finished accordingly

In general terms, a facade is the front face or public face of a building. Facade gives a nice outer look of the building by hiding the inner complex structure and makes it easily noticeable.

Similarly, there is a concept of facades in Laravel too. Which is used to wrap a complex library to provide a simpler and more readable interface. It makes syntax simple and easily memorable.

According to Laravel documentation,

“Facades provide a “static” interface to classes that are available in the application’s service container.”

In simple words,

A facade in Laravel is a wrapper around a non-static function that turns it into a static function.

In Laravel, we have seen many functions that are called statically. These all are examples of built facades.

DB::get('users')->all();

Route::get('/', function () { });

Config::get())

You can find more built facades in Laravel Documentation

Benefit of Using Laravel Facades

Laravel facades serve as static proxies to underlying classes in the service container. It provides easy and expressive syntax which is more maintainable, testable and flexible than traditional static methods.

Static vs Non-static in PHP

In definition, we touched the static function and non-static function. Letā€™s understand what static and non-static functions are in PHP.

What is a static function in PHP?

Static functions are those functions which do not depend on the instance of the class. In simple words, static functions do not require an instance of the class to execute.

Static methods use double colons (::) while accessing properties or methods of a class.

Example Code:

What is non-static function in PHP?

Non-static functions are those functions which depend on the instance of the class. In simple words, non-static functions require an instance of the class to execute.

Static methods use arrow operator (->) while accessing properties or methods of a class:

$this and class object name is used to reference properties or methods within a class.

Example Code:

Let me show you the comparison between static and non-static in tabular format.

No.Static FunctionNon-static function
1.Static functions do not require an instance of the class to execute.Non-static functions require an instance of the class to execute.
2.Double colons (::) while accessing properties or methods of a classArrow operator (->) while accessing properties or methods of a class
3.Reserved keywords like self , static and parents are used to reference properties or methods within a class$this is used to reference properties or methods within a class

To demonstrate, I will create one class to perform the mathematical operations add, substract, multiply and divide and will wrap this class with facade so that we can call functions of class statically.

Steps to Create Custom Facades

Before building and implementing your own custom facades. You need to have a clear picture of the files and folders structure in your mind for your project.

I will be creating following Calculator and Facades folders with Calculator.php and CalculatorFacade.php classes. Moreover, I will create CalculatorServiceProvider.php provider class inside existing Providers folder. Finally, I will make required changes in the config/app.php and rounts/web.php files.

For demo purpose, I will be using Laravel 9.

My project folders and files structure.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
| - app/
| - Calculator/
| | - Calculator.php
| | -Providers/
| | | - CalculatorServiceProvider.php
| | - Facades/
| | | - CalculatorFacade.php
| - config/
| | - app.php
| - rounts/
| | - web.php

Step #1 : Create Main PHP Class File

We will create aCalculator.php class which will be wrapped by the facade class. This class will have four non-static functions i.e. add, substract, multiply and device to perform the mathematical operations add, substract, multiply and device. I will create this class in App\Calculator namespace.

Example Code:

Step #2 : Create Facade Class

I will create facade class inside App\Facades namespace which extends Illuminate\Support\Facades\Facade. The protected static function getFacadeAccessor() function will return calculator string. This string need to be exactly same in the service provider register() function. This string can be anything.

Example Code:

Step #3 : Create Service Provide Class

Create your service provide class which extends Illuminate\Support\ServiceProvider. I have created CalculatorServiceProvider.php service provider class in my providers folder app/Providers Keep in mind this "calculator" must be exactly same as return from facades getFacadeAccessor() function while binding or wrapping an instance of your main class Calculator

Example Code:

Step #4 : Register Service Provider Class

Just creating service provider wonā€™t work. You need to register your Service Provider in the /Config/app.php config file, under the provider key.

Once you registered, you can now resolve the Calculator service from the container by using the global app() function.

You can also register Service Provider to Config\app.php as aliases. The Aliases can be used reference your class anywhere in your project.

It’s optional step.

Here in my project, I am registeringFacadesCalculator as alias for App\Facades\CalculatorFacade CalculatorFacade class. So that you can just call the alias and not the whole Namspaced ClassName. This alias name be valid string.

If you have registered alias you can use alias to call function like this : FacadesCalculator::add(1, 2);

Otherwise, you need to use whole namespace to call function App\Facades\CalculatorFacade::add(1, 2);

Example Code:

Step #5 : Run composer dump-autoload

After finishing all you need to run the composer dump-autoload in you project root directory. This command regenerates the list of all classes that need to be included in the project (autoload_classmap.php) It’s good to run when you have a new class inside your project.

Step #6 : Call non-static Functions Statically

You can call functions where ever you need in your project. Just to demonstrate, I will be calling functions from web.php router. So that we can see output in the browser screen.

I will call functions statically via facade wrapper as well as non-statically with class’s object.

Example Code:

View output

Run php artisan serve. This will sever your app in your localhost at port no 8000 (http://localhost:8000/)

How to create custom laravel facades

In this post, I have covered what is facades in Laravel, benefit of using facades, how to create your own custom facades and use them in Laravel applications in very simple steps with example. Also, I have discussed about static and non-static functions in PHP.

Twitter Post :