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

*文
Release: 2023-03-19 08:48:01
Original
1188 people have browsed it

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'); } }
Copy after login

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(); }
Copy after login

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" ...
Copy after login

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; } }
Copy after login

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,


        
Copy after login

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); } }
Copy after login

About class_alias Here is an official example:

class foo { } class_alias('foo', 'bar'); $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
Copy after login

Loading of Facade

When we are in When using Facade, for example:


        
Copy after login

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


        
Copy after login

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).

$method(...$args); }
Copy after login

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 'config'; }
Copy after login

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

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]; }
Copy after login

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

instance('config', $config = new Repository($items));
Copy after login

, so\Config::get('app.name', 'dafault)actually accessed It is theget('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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!