Facade and Service provider

Facade

Facades provide a "static" interface to classes that are available in the application's service container.

Create facade

Create the following files:
1. app/Services/Foobar.php
<?php
namespace App\Services;

class Foobar {
public function show() {
return 'Hello World';
}

}
2. app/Facades/Foobar.php
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Foobar extends Facade {
public static function getFacadeAccessor() {
return 'foobar';
}
}
3. app/Providers/FoobarServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\Foobar;
class FoobarServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('foobar', function() {
return new Foobar;
});
    }
}
4. Now, open the file config/app.php and add the service provider to providers array:
App\Providers\FoobarServiceProvider::class, 
5. Inside config/app.php add the alias of Foobar to aliases array:
'Foobar' => App\Facades\Foobar::class,
6. Add the following route into routes/web.php
Route::get('/foo', function () {
    return Foobar::show();
});
7. Now when you go to the '/foo' address you will get a 'Hello World' message.

Dig into deeper of facade

Foobar class (custom facade) contains getFacadeAccessor method.
This method returns the key (binding key like 'foobar'). This key is used in two place:
1. During binding any service class with service container using singleton class inside register method of your service provider. This key is like an Id.
2. Inside getFacadeAccessor method of your facade. This key is used by the Facade class (actually your facade) to get the instance of service.

Facade class contains __callStatic, getFacadeRoot, resolveFacadeInstance, getFacadeAccessor.

Here, getFacadeAccessor returns exception that 'Facade does not implement getFacadeAccessor method.'. That means, static::getFacadeAccessor method in parent(Facade class) will return the key (ex. 'foobar') which you've defined in your facade (ex. app/Facades/Foobar.php). Because static:: refers to your facade but not the Facade class as you invoked a method (ex. show) from your facade.

Here, getFacadeRoot() method returns the resolved instance of the service class using static::resolveFacadeInstance.
resolveFacadeInstance mehod basically returns the resolved instance of the service class. Look:
protected static function resolveFacadeInstance($name)
    {
        if (is_object($name)) {
            return $name;
        }
        if (isset(static::$resolvedInstance[$name])) {
            return static::$resolvedInstance[$name];
        }
        return static::$resolvedInstance[$name] = static::$app[$name];
    }
Here, $name is the key (binding key like 'foobar'). This method checks for $name as an object and returns $name object. The second if condition checks if it is first time or not. if first time the last line is executed which get value from service container using $app[$name]. After the first time second if condition is true.
Service container

Labels: ,

© copyright-2020 Rejaul