If you use Laravel more and more, then you will surely use Laravel Facades in your codes. For e.g, all the routes in the routes/web.php are facades even if you call any Eloquent model like User::all() in your project i.e also called facades. So, whats does this mean?

Facades are basically the way of accessing the instantiated class method in a static method way. For e.g. let's say we have a Student class in Classes/Student.php like this,

<?php

namespace App\Classes;


class Student
{
    public function method_1()
    {
        dump('in method 1');
    }
    
    public function method_2()
    {
        dump('in method 2');
    }
    
    public function method_3()
    {
        dump('in method 3');
    }

    public function method_4()
    {
        dump('in method 4');
    }
}

I want to access the methods of this class somewhere let's say in the routes file. So you will simply do this,

Route::get('/facades', function () {
    $student = new \App\Classes\Student();
    $student->method_1();
    $student->method_2();
    $student->method_3();
    $student->method_4();
});

But, if I access these methods in a static way then it will throw an error. Like,

/* all of thesse methods won't work */
Student::method_1();
Student::method_2();
Student::method_3();
Student::method_4();

This is where the facades came into play. This will help you to access all of the above methods statically. So now let's see how to make the facade and how we call it statically.

Let's Get Started

If you read the documentation, Laravel clearly says that it provides a static interface to classes that are available in the application's service container. That means we need one more class that will play the role of an interface and we need to bind that class in the service container.

Let's create one more class in the same directory where we created a normal class i.e. 'StudentFacade' which extends the 'Facade' class.

<?php

namespace App\Classes;

use Illuminate\Support\Facades\Facade;

class StudentFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        // just need to return the name of the binding you want to bind or already binded in the service container
        return 'student';
    }
}

In this class, we just need to override one static method i.e. getFacadeAccessor() which returns the name of a service container binding. This means that whenever you call the methods statically then Laravel will resolve this binding name in the service container.

Right now we don't have any binding. So, let's register one binding in the service container. I am using 'AppServiceProvider' you can create your own service provider as well.

<?php

namespace App\Providers;

use App\Classes\Student;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        // just need to return the new student class for the binding name
        $this->app->bind('student', function () {
            return new Student();
        });
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

As you see the provider's register method, I am using the same name for binding which I have returned in the getFacadeAccessor() method. You just need to return the instance of the class for this binding.

After that, you have successfully created Facade. Now, let's access the method statically.

Route::get('/facades', function () {
    \App\Classes\StudentFacade::method_1();
    \App\Classes\StudentFacade::method_2();
    \App\Classes\StudentFacade::method_3();
    \App\Classes\StudentFacade::method_4();
});

Now, check the browser and see the output.

Hence, you have successfully accessed all the method statically.

Last Modified: 1 month ago