[Laravel5.2 documentation] architecture-facade (Facades)
1. Introduction
The facade provides a "static" interface for the binding class in the service container of the application. Laravel has many built-in facades that you may be using without knowing. The facade of Laravel serves as the "static proxy" of the underlying class in the service container. compared with the traditional static method, it provides easier testing, more flexible, concise, and expressive syntax during maintenance.
2. use the facade
In the context of the Laravel application, the facade is a class that provides access to objects in the 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.
You only need to implement one method for the facade class: getFacadeAccessor. The getFacadeAccessor method defines what to parse from the container, and then the Facade base class uses the magic method _ callStatic () to call the parsing object from your Facade.
In the following example, we will call Laravel's Cache system. after browsing the code, you may think that we have called the static Cache method get:
$ User]) ;}}
Note that we have introduced the Cache facade at the top. This facade acts as a proxy to access the implementation of the underlying Illuminate \ Contracts \ Cache \ Factory interface. All calls to the door will be passed to the underlying instance of Laravel cache service.
If we look at the source code of the Illuminate \ Support \ Facades \ Cache class, we will find that there is no static method get:
Class Cache extends Facade {/*** get component registration name ** @ return string */protected static function getFacadeAccessor () {return 'cache ';}}
The Cache Facade inherits the Facade base class and defines the getFacadeAccessor method. the job of this method is to return the alias of the service container binding class. when you reference any static method of the Cache class, laravel parses the cache binding from the service container, and then calls all request methods (in this example, get) on the parsed object ).
3. Portal category list
The following lists each facade and its corresponding underlying classes, which is a useful tool for in-depth API documentation on the given root facade. The service container binding key is also included:
Facade |
Class |
Bind an alias to a service container |
App |
Illuminate \ Foundation \ Application |
App |
Artisan |
Illuminate \ Console \ Application |
Artisan |
Auth |
Illuminate \ Auth \ AuthManager |
Auth |
Auth (Instance) |
Illuminate \ Auth \ Guard |
|
Blade |
Illuminate \ View \ Compilers \ BladeCompiler |
Blade. compiler |
Bus |
Illuminate \ Contracts \ Bus \ Dispatcher |
|
Cache |
Illuminate \ Cache \ Repository |
Cache |
Config |
Illuminate \ Config \ Repository |
Config |
Cookie |
Illuminate \ Cookie \ CookieJar |
Cookie |
Crypt |
Illuminate \ Encryption \ Encrypter |
Encrypter |
DB |
Illuminate \ Database \ DatabaseManager |
Db |
DB (Instance) |
Illuminate \ Database \ Connection |
|
Event |
Illuminate \ Events \ Dispatcher |
Events |
File |
Illuminate \ Filesystem |
Files |
Hash |
Illuminate \ Contracts \ Hashing \ Hasher |
Hash |
Input |
Illuminate \ Http \ Request |
Request |
Lang |
Illuminate \ Translation \ Translator |
Translator |
Log |
Illuminate \ Log \ Writer |
Log |
Mail |
Illuminate \ Mail \ Mailer |
Mailer |
Password |
Illuminate \ Auth \ Passwords \ PasswordBroker |
Auth. password |
Queue |
Illuminate \ Queue \ QueueManager |
Queue |
Queue (Instance) |
Illuminate \ Queue \ QueueInterface |
|
Queue (Base Class) |
Illuminate \ Queue |
|
Redirect |
Illuminate \ Routing \ Redirector |
Redirect |
Redis |
Illuminate \ Redis \ Database |
Redis |
Request |
Illuminate \ Http \ Request |
Request |
Response |
Illuminate \ Contracts \ Routing \ ResponseFactory |
|
Route |
Illuminate \ Routing \ Router |
Router |
Schema |
Illuminate \ Database \ Schema \ Blueprint |
|
Session |
Illuminate \ Session \ SessionManager |
Session |
Session (Instance) |
Illuminate \ Session \ Store |
|
Storage |
Illuminate \ Contracts \ Filesystem \ Factory |
Filesystem |
URL |
Illuminate \ Routing \ UrlGenerator |
Url |
Validator |
Illuminate \ Validation \ Factory |
Validator |
Validator (Instance) |
Illuminate \ Validation \ Validator |
|
View |
Illuminate \ View \ Factory |
View |
View (Instance) |
Illuminate \ View |