How To Create Custom Facade Class In Laravel And Use It?

Sandeeppant
2 min readMar 25, 2025

--

Laravel provides a powerful Facade system that allows software engineers to access application services in a static-like manner. While Laravel comes with many built-in Facades, sometimes you need to create your own custom Facade to encapsulate business logic or reusable functionality. In this guide, we’ll walk through creating a custom Facade in Laravel.

Step 1: Create a Service Class

The first step is to create the service class that will contain the business logic. You can place this class anywhere, but Laravel convention suggests placing it in the App\Services namespace.

namespace App\Services;

class GreetingService
{
public function greet($name)
{
return "Hello, $name! Welcome to laravel.";
}
}

Step 2: Create a Facade Class

Next, create a Facade class that extends Illuminate\Support\Facades\Facade. This class serves as a static interface to the underlying service class.

Create a new file at app/Facades/SandeepFacade.php:

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class GreetingFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'greetingservice';
}
}

Step 3: Bind the Service in the Service Container

Laravel’s Facades rely on the service container. You need to bind your service to the container in a service provider. Open app/Providers/AppServiceProvider.php and register the binding inside the register method:

use App\Services\GreetingService;

public function register()
{
$this->app->singleton('greetingservice', function () {
return new GreetingService();
});
}

Step 4: Use the Custom Facade

Now, you can use your custom Facade anywhere in your application. For example, inside a controller:

use App\Facades\GreetingFacade;

class WelcomeController extends Controller
{
public function index()
{
return GreetingFacade::greet('Taylor Otwell');
}
}

Alternatively, you can use dependency injection instead of Facades:

use App\Services\GreetingService;

class WelcomeController extends Controller
{
public function index(GreetingService $greetingService)
{
return $greetingService->greet('taylor');
}
}

Advantages of Using a Custom Facade

  • Cleaner Code: Facades provide a simple, elegant, and readable way to access services.
  • Ease of Use: They allow you to call methods statically without worrying about manually resolving instances.
  • Encapsulation: You can centralize reusable business logic in a service class and expose it via a Facade.
  • Testability: Laravel provides ways to mock Facades, making unit testing easier.

Disadvantages of Using a Custom Facade

  • Hidden Dependencies: Since Facades use Laravel’s service container, dependencies might not be explicitly injected, making code harder to follow.
  • Static-Like Behavior: Although Facades resolve instances dynamically, their static interface can lead to confusion and limit flexibility.
  • Potential for Tight Coupling: Overusing Facades can make it harder to swap out implementations or refactor code.

Conclusion

Creating a custom Facade in Laravel is a straightforward process that involves defining a service class, creating a Facade class, binding the service in the container, and then using it anywhere in your application. Facades provide a clean and elegant way to access application services without manually resolving dependencies. However, it’s important to use them wisely to avoid potential downsides. Happy coding!

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response