How to create custom facades in Laravel?

In this article we will learn how to create custom facades in Laravel. But first we will know what facade is in Laravel.

What is facade in Laravel?

Facades provide a static interface to underlying classes in the service container. Facades also provide a memorable syntax to use Laravel’s features without remembering long class names that must be injected or configured manually.

All of Laravel’s facades are defined in the Illuminate\Support\Facades namespace.

In-built facades in Laravel

Some common in-built facades in Laravel are: Auth, Cache, Config, DB, File, Hash, Log, Mail, Request, Response, Route, Session, Storage etc.

How to use a facade in Laravel?

In our application we can access a facade as follows.

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
 
Route::get('/cache', function () {
    return Cache::get('key');
});

We can find more details of Laravel Facades in https://laravel.com/docs/10.x/facades.

Apart from the in-built facades in Laravel, we can create our own facade as per our requirement.

Create custom facades in Laravel

Now we will learn how we can create custom façade in Laravel.

We can create custom facade by following the steps below.

Steps to create custom facade

Step 1: Creating ‘Facades’ folder within app directory and Custom.php file in app/Facades

We will create a directory called Facades inside the app directory. We will also create a file named Custom.php in this directory.

Step 2: Adding the following code in app/Facades/Custom.php

We will add the following code in app/Facades/Custom.php.

<?php 
namespace App\Facades; 
use Illuminate\Support\Facades\Facade; 
class Custom 
{ 
public function greet() 
{ 
echo "Hello, Custom Facade"; 
} 
}
Step 3: Creating CustomServiceProvider using the following command.

We will create a service provider named CustomServiceProvider using the artisan command.

php artisan make:provider CustomServiceProvider

This will create AppServiceProvider.php file in app\Providers\AppServiceProvider.php.

Step 4: Binding the custom facade in AppServiceProvider

We will bind our custom facade in the service provider created above.

<?php
namespace App\Providers;
use App\Facades\Custom;
use Illuminate\Support\ServiceProvider;

class CustomServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->bind('custom',function(){
return new Custom();
});
}
public function boot(): void
{
//
}
}
Step 5: Registering the CustomServiceProvider within providers in config/app.php.

Now we will register the CustomServiceProvider within providers key in config/app.php.

...
'providers' => ServiceProvider::defaultProviders()->merge([
...
App\Providers\CustomServiceProvider::class,
])->toArray(),
..
Step 6: Creating another file CustomFacade.php in App\Facades

We will create another file named CustomFacade.php in App\Facades.

Step 7: Adding code in CustomFacade.php

Here we will add a class CustomFacade which will extend Illuminate\Support\Facades\Facade.

<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class CustomFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'custom';
}
}
Step 8: Now registering the CustomFacade class within aliases in config/app.php.

Now we will register the CustomFacade class within aliases in config/app.php.

...
'aliases' => Facade::defaultAliases()->merge([
...
'Custom' => App\Facades\CustomFacade::class,
])->toArray(),
]
Step 6: Using the custom facade

Finally we get to use the facade that we created. We can add the following route in routes/web.php.

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

Now, if we browse http://<SITEURL>/greet, the output will be: Hello, Custom Facade.

The output is this because the greet method of the app/Facades/Custom.php file displays this echo statement.