The Facade operating mechanism Facade provides a "static" interface for the binding class in the application service container. This mechanism is implemented by the Facade class. the Facade provided by Laravel and the created custom Facade will inherit from the Illuminate \ Support \ Facades \ Facade base class.
(Use Route for example)
Mechanism flowchart:
Top-level facade call
Config/app. php 'aliases' => ['app' => Illuminate \ Support \ Facades \ App: class, 'artism' => Illuminate \ Support \ Facades \ Artisan: class, 'auth' => Illuminate \ Support \ Facades \ Auth: class, 'Blade '=> Illuminate \ Support \ Facades \ Blade: class, 'cache' => Illuminate \ Support \ Facades \ Cache: class, 'config' => Illuminate \ Support \ Facades \ Config: class, 'cookies' => Illuminate \ Support \ Facades \ Cookie: class, 'Crypt' => Illuminate \ Support \ Facades \ Crypt: class, 'DB' => Illuminate \ Support \ Facades \ DB: class, 'eloquent' => Illuminate \ Database \ Eloquent \ Model: class, 'event' => Illuminate \ Support \ Facades \ Event: class, 'file' => Illuminate \ Support \ Facades \ File: class, 'gate' => Illuminate \ Support \ Facades \ Gate: class, 'hash' => Illuminate \ Support \ Facades \ Hash: class, 'Lang '=> Illuminate \ Support \ Facades \ Lang: class, 'log' => Illuminate \ Support \ Facades \ Log: class, 'Mail' => Illuminate \ Support \ Facades \ Mail: class, 'password' => Illuminate \ Support \ Facades \ Password: class, 'queue '=> Illuminate \ Support \ Facades \ Queue: class, 'redirect' => Illuminate \ Support \ Facades \ Redirect: class, 'redis '=> Illuminate \ Support \ Facades \ Redis: class, 'request' => Illuminate \ Support \ Facades \ Request: class, 'response' => Illuminate \ Support \ Facades \ Response: class, 'Route '=> Illuminate \ Support \ Facades \ Route: class, // for example, let's look at Route, generally, we call Route: get to call the alias Route. then, this Route points to the class Illuminate \ Support \ Facades \ Route. Why can this mechanism be implemented, continue to analyze 'scheme' => Illuminate \ Support \ Facades \ Schema: class, 'session '=> Illuminate \ Support \ Facades \ Session: class, 'storage' => Illuminate \ Support \ Facades \ Storage: class, 'URL' => Illuminate \ Support \ Facades \ URL: class, 'validator' => Illuminate \ Support \ Facades \ Validator: class, 'View' => Illuminate \ Support \ Facades \ View: class, 'form' => Collective \ Html \ FormFacade: class, 'html' => Collective \ Html \ HtmlFacade: class,],
Route. php
(If phpstome is used, comm + B can find the source code of the class through the class name)
Vendor/laravel/framework/src/Illuminate/Support/Facades/Route. php <? Phpnamespace Illuminate \ Support \ Facades;/*** @ see \ Illuminate \ Routing \ Router */class Route extends Facade {/*** Get the registered name of the component. ** @ return string */protected static function getFacadeAccessor () // only knows that the Route class inherits Facade and has a static method getFacadeAccessor, which returns a router string, this is a static method, {return 'router ';}}
When we use Route: get ();, we can see that there is no static get method here, so we will call the magic method _ callStatic of Facade.
Go to the Facade source code again and find a static magic method named _ callStatic.
_ CallStatic: automatically executed when calling a static method that does not exist in the object;
Vendor/laravel/framework/src/Illuminate/Support/Facades/Facade. phppublic static function _ callStatic ($ method, $ args) {$ instance = static: getFacadeRoot (); // after the magic method is called, call a key getFacadeRoot method again, this is a static call if (! $ Instance) {throw new RuntimeException ('a facade root has not been set. ');} switch (count ($ args) {case 0: return $ instance-> $ method (); case 1: return $ instance-> $ method ($ args [0]); case 2: return $ instance-> $ method ($ args [0], $ args [1]); case 3: return $ instance-> $ method ($ args [0], $ args [1], $ args [2]); case 4: return $ instance-> $ method ($ args [0], $ args [1], $ args [2], $ args [3]); default: return call_user_func_array ([$ instance, $ method], $ args );}}Enter the source code of the getFacadeRoot method again (in fact, it is also the current facade file)
Vendor/laravel/framework/src/Illuminate/Support/Facades/Facade. php/*** The resolved object instances. ** @ var array */protected static $ resolvedInstance; // This is a storage instance, which is used to parse the public static function getFacadeRoot () {return static :: resolveFacadeInstance (static: getFacadeAccessor (); // Two methods are run. getFacadeAccessor is passed as the resolveFacadeInstance parameter}/*** Get the registered name of the component. ** @ return string ** @ throws \ RuntimeException */protected static function getFacadeAccessor () // returns and throws an error message {throw new RuntimeException ('facade does not implement getFacadeAccessor method. ');}/*** Resolve the facade root instance from the container. ** @ param string | object $ name * @ return mixed */protected static function resolveFacadeInstance ($ name) // This method requires a $ name variable, this variable is getFacadeAccessor (), that is, in the vendor/laravel/framework/src/Illuminate/Support/Facades/Route. php returns a string because of route. php overrides the getFacadeAccessor method, so it can obtain {if (is_object ($ name) {return $ name;} if (isset (static: $ resolvedInstance [$ name]). {return static: $ resolvedInstance [$ name];} return static: $ resolvedInstance [$ name] = static ::$ app [$ name]; // here, $ app is used to parse the objects in the INER. the previously mentioned IOC put the obtained keyword to $ app [] for parsing, store the resolved INER object to $ resolvedInstance, and then return}