Home  >  Article  >  Backend Development  >  Detailed explanation of the loading process and principle of Facade in Laravel

Detailed explanation of the loading process and principle of Facade in Laravel

*文
*文Original
2018-01-03 17:16:551248browse

Facade is actually a static proxy of a class in the container. It allows you to statically call any method of any object stored in the container. This article mainly introduces you to the loading process and principle of Facade in Laravel. For relevant information, friends in need can refer to it. I hope to be helpful.

Preface

This article mainly introduces the relevant content about the Facade loading process and principles in Laravel, and shares it for your reference and study. Not much to say below, let’s take a look at the detailed introduction.

Introduction

Facades (pronounced: /fəˈsäd/) provide a " Static" interface. You don't have to use a bunch of namespaces or instantiate the object to access its specific methods.

use Config;

class Test
{
 public function index()
 {
 return Config::get('app.name');
 }
}

Facade startup and registration

Facade startup boot is registered in Illuminate\Foundation\Bootstrap\RegisterFacades.

public function bootstrap(Application $app)
{
 Facade::clearResolvedInstances();
 Facade::setFacadeApplication($app);

 AliasLoader::getInstance(array_merge(
 $app->make('config')->get('app.aliases', []),
 $app->make(PackageManifest::class)->aliases()
 ))->register();
}

The default alias configuration is read from aliases under the app configuration file. PackageManifest is a new package automatic discovery rule added by laravel 5.5. Here we do not consider the aliases provided by the PackageManifest package for the time being.

Among them, array_merge returns an array in the following format:

 "App" => "Illuminate\Support\Facades\App"
 "Artisan" => "Illuminate\Support\Facades\Artisan"
 "Auth" => "Illuminate\Support\Facades\Auth"
 "Blade" => "Illuminate\Support\Facades\Blade"
 ...

The above code will register all facades for automatic loading through AliasLoader. The core is php's spl_autoload_register.

 /**
 * Prepend the load method to the auto-loader stack.
 *
 * @return void
 */
 protected function register()
 {
 if (! $this->registered) {
  spl_autoload_register([$this, 'load'], true, true);

  $this->registered = true;
 }
 }

After the registration is completed, all subsequent use classes will be automatically loaded through the load function.

Note: When defining spl_autoload_register here, the last parameter is passed true. When this parameter is true, spl_autoload_register() will add the function to the head of the queue instead of the tail. (This function is used first to complete automatic loading)

In other words,

<?php

use Config;
use App\User;

class Test
{
 public function index()
 {
 Config::get(&#39;app.name&#39;);
 new User();
 }
}

No matter what we use is a specific existing class (App\User) or an alias (Config), Automatic loading will be completed first through the load function. When the function returns false, automatic loading will be completed by other automatic loading functions (such as composer psr-4).

In the load method of AliasLoader, the class_alias function is mainly used to implement automatic loading of aliases.

public function load($alias)
{
 if (isset($this->aliases[$alias])) {
 return class_alias($this->aliases[$alias], $alias);
 }
}

About class_alias Here is an official example:

class foo { }

class_alias(&#39;foo&#39;, &#39;bar&#39;);

$a = new foo;
$b = new bar;

// the objects are the same
var_dump($a == $b, $a === $b); //true
var_dump($a instanceof $b); //false

// the classes are the same
var_dump($a instanceof foo); //true
var_dump($a instanceof bar); //true

var_dump($b instanceof foo); //true
var_dump($b instanceof bar); //true

Loading of Facade

When we are in When using Facade, for example:

<?php

use Config;

class Test
{
 public function index()
 {
 Config::get(&#39;app.name&#39;);
 }
}

actually loads the Illuminate\Support\Facades\Config class (because we have registered class_alias), which is equivalent to:

<?php

use Illuminate\Support\Facades\Config;

class Test
{
 public function index()
 {
  Config::get(&#39;app.name&#39;);
 }
}

And all Facades are Inherited from the Illuminate\Support\Facades\Facade class, a __callStatic method is defined in this base class, so that we can easily use Facade (without instantiation).

<?php

public static function __callStatic($method, $args)
{
 $instance = static::getFacadeRoot();

 if (! $instance) {
  throw new RuntimeException(&#39;A facade root has not been set.&#39;);
 }

 return $instance->$method(...$args);
}

The getFacadeRoot method is used to obtain the specific instance column of the alias class. We know that all Facade classes need to define a getFacadeAccessor method. Possible return values ​​of this method are:

  • String type string (such as config, db)

  • String type string-like ( Such as App\Service\SomeService)

  • Object specific instantiated object

  • Closure closure

For example, the getFacadeAccessor method of Config Facade is as follows:

protected static function getFacadeAccessor()
{
 return &#39;config&#39;;
}

The getFacadeRoot method will retrieve the corresponding real column object from the container based on the return value of getFacadeAccessor() .

public static function getFacadeRoot()
{
 $name = static::getFacadeAccessor();
 
 if (is_object($name)) {
  return $name;
 }

 if (isset(static::$resolvedInstance[$name])) {
  return static::$resolvedInstance[$name];
 }

 return static::$resolvedInstance[$name] = static::$app[$name];
}

Since the actual column of config has been registered in the APP container

<?php
//Illuminate\Foundation\Bootstrap/LoadConfiguration

$app->instance(&#39;config&#39;, $config = new Repository($items));

, so \Config::get('app.name', 'dafault) actually accessed It is the get('app.name', 'default') method of the Repository actual column.

Related recommendations:

##Detailed explanation of Laravel using salt and password authentication by modifying Auth

Detailed explanation of Laravel’s localization module

Detailed explanation of how to rewrite resource routing in Laravel

The above is the detailed content of Detailed explanation of the loading process and principle of Facade in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn