Documentation Index
Fetch the complete documentation index at: https://kawax.biz/llms.txt
Use this file to discover all available pages before exploring further.
What are facades?
Facades provide a “static” interface to classes available in the service container. Laravel ships with facades that cover almost every feature of the framework, all living in theIlluminate\Support\Facades namespace:
You don’t need to understand every detail of how facades work before using them. Follow the flow, keep learning, and come back to this page when you want to dig deeper.
How facades work
In a Laravel application, a facade is a class that provides access to an object from the container. The mechanism lives in the baseIlluminate\Support\Facades\Facade class, which every facade extends.
The base class uses PHP’s __callStatic() magic method to forward calls from your facade to an object resolved from the container. Here’s an example:
Cache facade class, there is no static get method defined:
getFacadeAccessor() returns the service container binding key. When you call Cache::get(), Laravel resolves the cache binding from the container and calls get() on that object. The static call is just syntactic sugar.
When to use facades
Facades offer a short, memorable syntax for using Laravel’s features without manually injecting or configuring class names. Because PHP’s dynamic methods power them behind the scenes, they are easy to test too.Watch out for scope creep
The main risk with facades is class “scope creep.” Because facades are so easy to use and need no injection, you might keep adding them to a class without noticing it growing too large. With dependency injection, a long constructor serves as a visual warning that a class has too many responsibilities.Facades vs. dependency injection
A key benefit of dependency injection is the ability to swap implementations during testing—just inject a mock or stub. Since facades proxy calls to objects resolved from the container, you can test them the same way:Cache::get is called with the right argument:
- Pest
- PHPUnit
Facades vs. helper functions
Laravel also provides global helper functions for common tasks. In many cases, a helper and its corresponding facade are equivalent:Real-time facades
Real-time facades let you treat any class in your application as if it were a facade—without creating a dedicated facade class. To demonstrate, consider aPodcast model that needs a Publisher to publish itself:
publish must pass a Publisher instance explicitly. With a real-time facade, prefix the imported namespace with Facades\:
Facades\. Testing remains straightforward:
- Pest
- PHPUnit
Testing facades
UseshouldReceive to set expectations on a facade. It returns a Mockery mock instance:
| Method | Purpose |
|---|---|
shouldReceive('method') | Expect the method to be called |
once() | Expect exactly one call |
times(n) | Expect exactly n calls |
with(args) | Expect specific arguments |
andReturn(value) | Return the given value |
andReturnNull() | Return null |
Facade class reference
Every built-in facade with its underlying class and service container binding:| Facade | Class | Binding |
|---|---|---|
App | Illuminate\Foundation\Application | app |
Auth | Illuminate\Auth\AuthManager | auth |
Cache | Illuminate\Cache\CacheManager | cache |
Config | Illuminate\Config\Repository | config |
Cookie | Illuminate\Cookie\CookieJar | cookie |
Crypt | Illuminate\Encryption\Encrypter | encrypter |
DB | Illuminate\Database\DatabaseManager | db |
Event | Illuminate\Events\Dispatcher | events |
File | Illuminate\Filesystem\Filesystem | files |
Gate | Illuminate\Contracts\Auth\Access\Gate | — |
Hash | Illuminate\Contracts\Hashing\Hasher | hash |
Http | Illuminate\Http\Client\Factory | — |
Log | Illuminate\Log\LogManager | log |
Mail | Illuminate\Mail\Mailer | mailer |
Notification | Illuminate\Notifications\ChannelManager | — |
Queue | Illuminate\Queue\QueueManager | queue |
RateLimiter | Illuminate\Cache\RateLimiter | — |
Redirect | Illuminate\Routing\Redirector | redirect |
Request | Illuminate\Http\Request | request |
Route | Illuminate\Routing\Router | router |
Schema | Illuminate\Database\Schema\Builder | — |
Session | Illuminate\Session\SessionManager | session |
Storage | Illuminate\Filesystem\FilesystemManager | filesystem |
URL | Illuminate\Routing\UrlGenerator | url |
Validator | Illuminate\Validation\Factory | validator |
View | Illuminate\View\Factory | view |
Service Container
Go deeper on how the container resolves classes and manages dependencies.