laravel facade tutorial | Vegi Bit
The Facade?
Do you want to know what it is?
Facades are everywhere, they are all around our code. Even now in this very simple snippet
There is a Facade which blinds you from the truth.
What truth?
That you have picked up horrible habits on your journey of spaghetti code PHP in your quest to build websites and applications. That you are now a slave to this prison which has been created. Ultimately, no one can be told what the Facade is. You have to see it for yourself. This is your last chance, after this, there is no turning back:
You had to go and take the Red Pill, didn’t you…
You have chosen to see the truth, and we will provide you that, nothing more…
With a nod to the Matrix, we’ll investigate Facades.In the matrix the system was the enemy, however in Laravel, Facades are actually your friend. You’ll notice that a lot of the coding done in Laravel looks like it is calling static methods to give it’s nice and short syntax. The truth is, that is not what is happening, Laravel is making use of it’s Facade class to give you this great syntax, while also at the same time, employing the IoC Container to instantiate objects and any dependencies for you to use. So you can see, the short syntax acts almost like a macro, or shortcut. While they look like static classes, they actually are not. What this does is decouple your code from the implementation.
Format of a Facade
Most times in Laravel, you will see something similar to, or following the format ofComponent::verb(). The component would be something like Route
or DB
, and the verb would be something describing the action taken on that component like get() or select().
How the Facade Works
If you navigate to the vendorlaravelframeworksrcIlluminateSupportFacades
directory in your Laravel install, you will see a collection of Facade class files such as App
Artisan
Auth
Blade
Cache
Config
Cookie
Crypt
DB
Event
Facade
File
Form
Hash
HTML
Input
Lang
Log
Mail
Paginator
Password
Queue
Redirect
Response
Route
Schema
Session
SSH
URL
Validator
and View
. All of these extend the parent Facade
class.
getFacadeAccesor()
All of these class files extend the Facade
class, and act as support classes to get access to an object in the IoC Container. All of these class files that extend Facade
have a method namedgetFacadeAccesor()
. The job of this method is to inform what object gets resolved from the IoC Container. So in our Route
Facade this line:
returns the string router, so that is what gets resolved and made available to us.
The full Route
Facade looks like this
__callStatic($method, $args)
In the parent Facade
class, this method gets called when a static method that does not exist on a class is called. So when you call Route::get()
, the Facade class transforms this into something like $app['router']->get('/', function()
which is to say: Resolve router out of the IoC Container and then call the get method. This provides a great deal of flexibility in your code.
In our Laravel CRUD tutorial, we were making use of the DB
facade and calling all kinds of methods like DB::table()
DB::insert()
DB::select()
DB::update()
and DB::delete()
. All of these are examples of using this same Facade pattern. Our DB
Facade contains this line:
Just like the Route
example above, the method returns the string that identifies what object will get resolved. So again when we call:
Laravel will work it’s magic and actually run something more like:
Roll Your Own Facade
Personally, I like to stick with what the masters before me have created, but let’s say you are more of the adventurous type. If you wanted a different implementation of the DB class in your application, you could do that. Maybe you and your team come up with a database class capable of slaying Agent Smith with Kung Fu powers. This class is called KungFuData. If you would like to use KungFuData in your application instead of the baked in version, you can simply do this:
KungFuData is now directly accessible from the container, and the DB Facade will now use KungFuData as well. You can replace implementations in Laravel, and this new class will be used throughout your entire application.
In Summary
- Every Facade class uses
getFacadeAccessor()
that returns a string to identify the object to resolve - The parent Facade class uses
__callStatic()
to trigger the resolution - The use of Laravel’s Facade pattern provides us with pretty, elegant, static syntax. This helps reduce some of the overhead you would normally have to deal with each time you want to use an object. For example, creating new objects with the new keyword, assigning initial setup to the object, placing that implementation into a variable, and then finally accessing methods and properties doing something like
$myobject->mymethod()
or$myobject->myproperty
- Maintains ability to Test Code. Integration with Mockery is built into the Facade class to allow for quick and easy object mocking.
- Easily Switch API Versions
- Thanks to the
Facade
class we can use DB::insert() or Route::get() and Laravel will do all the heavy lifting for us by instantiating our objects and calling the right methods automagically. We don’t have to use $app[‘db’]->insert() or $app[‘router’]->get(), Laravel will do this for you.
Wow, this is quite the Rabbit Hole we have here in Wonderland, yes Neo?