Laravel Facades are classes that provide a static interface and access to an object from the container.
Laravel facades and any facades we create extend the base "Illuminate\Supports\Facades\Facade
" class.
The Facade base class uses the __callStatic()
magic method to defer calls from the facade to an object resolved from the container.
Each facade class has a method called getFacadeAccessor()
that has a job to return the name of the service container binding.
When a user references any static methods on the Cache facade, for example, Laravel resolves the "cache" binding from the container and runs the requested method against that object. ("cache" in this example, is the name that Cache facade's getFacadeAccessor() method returns).
To associate a Facade with a class in the service container, you must add an entry in the "aliases" array in the "config/app.php
" configuration file. The entry's key is the name of the Facade, and the value is the fully qualified class name that should be returned when the Facade is accessed.
When a Facade is used in code, it appears as if you are directly accessing a static method on a class.
– In reality, the Facade is a convenient way of accessing a class instance managed by the service container. This makes it easy to swap out implementations of a class without affecting the rest of the application.
All Comments