Search

Laravel Facades Part One: An Introduction to Facades

November 21, 2016 —John Koster

This article is part of a four part series all about Laravel facades. Each of the parts are listed below:

Facades are a convenient way to access Laravel's components. Each facade is bound to some component already registered in the service container. Facades are typically used to provide a simpler interface to an complex underlying subsystem. While some facades provide "shortcut" methods, most of Laravel's facades act like proxies by providing a static interface to an actual class instance.

All of the default facades are located within the Illuminate\Support\Facades namespace and can be located within the vendor/laravel/framework/src/Illuminate/Support/Facades directory. Every facade extends the abstract Facade (Illuminate\Support\Facades\Facade) class and must provide a getFacadeAccessor() method which is defined to be protected.

The getFacadeAccessor() method indicates what class or component should be resolved from the service container when the facade is accessed. All method calls are then redirected to that class instance. Laravel refers to this class instance as the facade root.

Some facades define additional static methods that may are not necessarily part of the underlying component or class. In these instances, the static method (that belongs to the facade) is called directly. One such example is the Cookie facade.

Facade roots are the class instance that is bound in the service container. For example, the Auth facade redirects all method calls to an instance of Illuminate\Auth\AuthManager, which the service container resolves with the auth binding.

#Facade Aliases and Importing Facades

Each facade has an alias. An alias in PHP is just a shortcut to a longer class name. For example, when we use something as something else, we are creating a class alias:

1<?php
2use Some\ReallyReallyLongClassName as ShorterName;

In the above example ShorterName is an alias of ReallyReallyLongClassName. Laravel creates aliases for all of the facades automatically, and the entire list of them can be found in the aliases array in the config/app.php file. The aliases that Laravel creates are in the global namespace, which means that developers can write code that looks like this:

1<?php
2 
3namespace App\Http\Controllers;
4 
5use Input;
6 
7// Other code here.

instead of having to write code that references the full namespace:

1<?php
2 
3namespace App\Http\Controllers;
4 
5use Illuminate\Support\Facades\Input;
6 
7// Other code here.
Fully Qualified Names vs. Aliases

Fully qualified names, such as Illuminate\Support\Facades\Input are longer, but offer greater clarity on what class is being imported. At the end of the day, it comes down to personal preference.

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.