'How does this Facade works

I'm working with Laravel 5.8 and it's an Online Store project written by other programmers.

Basically I have faced something weird that never seen before.

Let's say we have this at a Controller method:

$payment = CourseRegistrationFacade::insertCourseRegisterInformation($courseId,$date,$memberId,$userId);

And then we goto the CourseRegistrationFacade and it goes like this:

class CourseRegistrationFacade extends BaseFacade
{
    
}

So the whole class is empty but it extends another Facade which is BaseFacade and it goes like this:

class BaseFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return static::class;
    }

    protected static function shouldProxyTo($class)
    {
        app()->bind(self::getFacadeAccessor(), $class)
    }
}

And that's it !

I don't really know where the heal is insertCourseRegisterInformation !!

So if you know how this Facade works, please let me know...


Here is the full code of BaseFacade.php:

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class BaseFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return static::class;
    }

    public static function shouldProxyTo($class)
    {
        app()->bind(self::getFacadeAccessor(), $class);
    }
}


Solution 1:[1]

Search in the code for:

CourseRegistrationFacade::shouldProxyTo(

Most likely in the service provider that line is somewhere registering that facade to some concrete implementation of a class. Then check the contents of the class (the argument passed to shouldProxyTo).

Inside that class there should be a method called insertCourseRegisterInformation.

The way facades work is they resolve the class out of the container and then call the method you call statically.

So for example, let's say you have a UserService.php with a method register() and that class is mapped to a UserServiceFacade.php. When you do UserServiceFacade::register(), __callStatic will get the facade accessor (actual class) from the container, then call the register() method of that class.

You can understand better by inspecting __callStatic inside Facade.php.

Essentially UserServiceFacade::register() is the same as doing:

$userService = app()->make(UserService::class);

$userService->register()

By using the facade you can hide the concrete implementation and could possibly switch it to something else in the future by just changing it in a single place.

Solution 2:[2]

I think there must be a Provider exists for that Facade which is initializing its associated class and insertCourseRegisterInformation method definition must be declared in it. Please find that provider and then you'll find its associated class from that Provider code. I think you can find all registered providers from config/app.php

These reference articles might help you.

Reference 1: https://grafxflow.co.uk/blog/mvc/adding-global-custom-class-facades-laravel-5

Reference 2: http://www.expertphp.in/article/how-to-create-custom-facade-in-laravel-52

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 khawar Ali