Search

Laravel Facades Part Four: Facade Class Reference

November 21, 2016 —John Koster

The following tables will list all the facades that are available by default. In addition, they will display the name of the service container binding as well as the class behind the facade. If a particular facade provides additional methods (that are not necessarily available in the underlying class), it will appear in the third table "Facades Providing Additional Methods". Any additional methods will be explained later in the section.

Facade Service Container Binding
App app
Artisan Illuminate\Contracts\Console\Kernel
Auth auth
Blade See "Notes on Blade"
Bus Illuminate\Contracts\Bus\Dispatcher
Cache cache
Config config
Cookie cookie
Crypt encrypter
DB db
Event events
File files
Gate Illuminate\Contracts\Auth\Access\Gate
Hash hash
Input request
Lang translator
Log log
Mail mailer
Password auth.password
Queue queue
Redirect redirect
Redis redis
Request request
Response Illuminate\Contracts\Routing\ResponseFactory
Route router
Schema See "Notes on Schema"
Session session
Storage filesystem
URL url
Validator validator
View view
Facade Resolved Class
App Illuminate\Foundation\Application
Artisan App\Console\Kernel
Auth Illuminate\Auth\AuthManager
Blade Illuminate\View\Compilers\BladeCompiler
Bus Illuminate\Bus\Dispatcher
Cache Illuminate\Cache\CacheManager
Config Illuminate\Config\Repository
Cookie Illuminate\Cookie\CookieJar
Crypt Illuminate\Encryption\Encrypter
DB Illuminate\Database\DatabaseManager
Event Illuminate\Events\Dispatcher
File Illuminate\Filesystem\Filesystem
Gate Illuminate\Auth\Access\Gate
Hash Illuminate\Hashing\BcryptHasher
Input Illuminate\Http\Request
Lang Illuminate\Translation\Translator
Log Illuminate\Log\Writer
Mail Illuminate\Mail\Mailer
Password Illuminate\Auth\Passwords\PasswordBroker
Queue Illuminate\Queue\QueueManager
Redirect Illuminate\Routing\Redirector
Redis Illuminate\Redis\Database
Request Illuminate\Http\Request
Response Illuminate\Routing\ResponseFactory
Route Illuminate\Routing\Router
Schema Illuminate\Database\Schema\MySqlBuilder
Session Illuminate\Session\SessionManager
Storage Illuminate\Contracts\Filesystem\Factory
URL Illuminate\Routing\UrlGenerator
Validator Illuminate\Validator\Factory
View Illuminate\View\Factory
Facade Number of Additional Methods
Cookie 2
Input 2
Schema 2

#Notes on Blade

Most facades request a concrete class implementation from the service container based off of some abstract string representation. However, the Blade facade retrieve an Illuminate\View\Engines \EngineResolver instance from the Illuminate\View\Factory.

The Illuminate\View\Engines\EngineResolver is a class that returns template compilers based on a given key name. By default, the following compilers and engines are available:

Compiler/Engine Name Concrete Class Implementation
php Illuminate\View\Engines\PhpEngine
blade Illuminate\View\Compilers\BladeCompiler

Developers can manually create an instance of the BladeCompiler themselves like so (this sample is provided for demonstration purposes):

1<?php
2 
3use Illuminate\Support\Facades\App;
4 
5$bladeCompiler = App::make('view')->getEngineResolver()
6 ->resolve('blade')->getCompiler();

#Notes on Schema

Like the Blade facade, the Schema facade does not simply resolve some instance from the service container. The Schema facade returns an instance of Illuminate\Database\Schema\Builder configured to use the default connection that appears in the database database configuration.

#Additional Cookie Methods

The Cookie facade defines two additional methods. These methods access other, related, components. These methods exist to simplify accessing related components.

#has($key)

The has function will check if a cookie with the given $key exists for the current request.

#get($key, $default = null)

The get function will retrieve a cookie from the current request with the given $key. A $default value can be supplied and will be returned if a cookie with the given $key does not exist.

#Additional Input Methods

The Input facade defines one extra method. Facades define extra methods to provide simpler access to underlying sub-systems, or to call functions on other, related components.

#get($key = null, $default = null)

The get method will get an item from the input data, such as when a user posts a form or an API request is being processed. The get method can be used for requests with the following HTTP verbs:

  • GET
  • POST
  • PUT
  • DELETE

The get method will invoke the input method on an instance of the Illuminate\Http\Request class.

The get method looks up the data based on the given $key. A $default value can be supplied and will be returned if the given $key is not found in the request data.

#Additional Schema Methods

The Schema facade defines one extra method. Facades define extra methods to provide simpler access to underlying sub-systems, or to call functions on other, related components.

#connection($name)

The connection method will return a new schema builder (Illuminate\Database\Schema\Builder) instance for the given connection. The $name is the name of the connection as it appears in the database configuration file.

#Resolving the Class Behind a Facade

It is possible to quickly resolve the class behind a facade. Facades expose a public method getFacadeRoot which will return the instance of the underlying object the facade is forwarding method calls to. It is convenient that getFacadeRoot returns an object instance because PHP's get_class method can then be used to retrieve the fully-qualified name of the facade's underlying class implementation.

1// Getting the class name of the underlying facade instance.
2$className = get_class(Auth::getFacadeRoot());

In the above code example, the $className variable would contain the value Illuminate\Auth \AuthManager.

This method of determining a facade's underlying class can be expanded on to create a function that will list every facade's underlying class for the current Laravel installation:

1/**
2 * Generates an HTML table containing all registered
3 * facades and the underlying class instances.
4 *
5 * @return string
6 */
7function getFacadeReferenceTable()
8{
9 $html = '';
10 
11 // An array of all the facades that should be printed in the table.
12 $facades = [
13 'App', 'Artisan', 'Auth', 'Blade', 'Bus',
14 'Cache', 'Config', 'Cookie', 'Crypt', 'DB',
15 'Event', 'File', 'Hash', 'Input', 'Lang', 'Log',
16 'Mail', 'Password', 'Queue', 'Redis', 'Redirect',
17 'Request', 'Response', 'Route', 'Schema', 'Session',
18 'Storage', 'URL', 'Validator', 'View'
19 ];
20 
21 // Boilerplate HTML to open an HTML table.
22 $html = '<table><thead><tr><th>Facade</th>';
23 $html .= '<th>Underlying Class</th></tr><tbody>';
24 
25 foreach ($facades as $facade)
26 {
27 $html .= '<tr><td>',$facade,'</td><td>';
28 $html .= get_class(call_user_func($facade.'::getFacadeRoot'));
29 $html .= '</td></tr>';
30 }
31 
32 // Boilerplate HTML to close an HTML table.
33 $html .= '</tbody></table>';
34 
35 return $html;
36}

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.