1. Introduction
The façade provides a "static" interface for the binding class in the application's service container. Laravel has many facades built in, and you may be using them without knowing. Laravel's façade serves as a "static proxy" for the underlying class in the service container, providing easier to test, more flexible, concise, and expressive syntax than traditional static methods in maintenance.
2. Use the façade
In the context of the Laravel application, the façade is a class that provides access to objects in the container. The mechanism is implemented by the facade class, and the Laravel comes with the façade, and the custom façade that is created, inherits from the Illuminate\support\facades\facade base class.
The façade class only needs to implement one method: Getfacadeaccessor. It is the Getfacadeaccessor method that defines what is parsed from the container and then facade the base class using the Magic Method __callstatic () to invoke the parsed object from your façade.
In the following example, we will call the Laravel cache system, and after browsing the code, you might think that we have called the cache's static method get:
$user]);} }
Note that we introduced the cache façade in the top position. The façade acts as an agent to access the implementation of the underlying Illuminate\contracts\cache\factory interface. All calls to the façade will be passed to the underlying instance of the 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 façade inherits the facade base class and defines the Getfacadeaccessor method, which works by returning the alias of the service container binding class, and when the user references any static method of the cache class, Laravel resolves the cache binding from the service container. Then all the request methods (in this case, get) are invoked on the parsed object.
3. List of façade classes
Each façade and its underlying class are listed below, which is a useful tool for delving into the API documentation for a given root façade. The service container binding key is also included:
Façade |
class |
service Container binding aliases |
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 |
Cookies |
Illuminate\cookie\cookiejar |
Cookies |
Crypt |
Illuminate\encryption\encrypter |
Encrypter |
Db |
Illuminate\database\databasemanager |
Db |
DB (Instance) |
Illuminate\database\connection |
|
Event |
Illuminate\events\dispatcher |
Events |
File |
Illuminate\filesystem\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\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\view |