Real-time Facades have been available since version 5 of Laravel; however, I haven't used them much until now. Since I'm just discovering how these work, I thought it would be helpful to do a quick write-up for you 😉.
What Are Facades
Before understanding Real-time Facades, it would be helpful to have a basic knowledge of Facades. What are Facades in Laravel, and how are they used?
A Facade is a "static" interface for a class. In other words, it allows you to call methods from a class statically. Here's an example of the Route
Facade Class that we know and love:
Route::get('facades', function(){
echo "The Route Class is a Facade";
});
You can take a look at all the Facade Classes provided in the default Laravel Service Container at https://laravel.com/docs/facades#facade-class-reference
Creating A Facade
It's even more helpful if you see an example of how we can create a new Facade in Laravel. If we are working with a new Laravel app, we could make two new classes inside our app\Facades
folder.
Greeting.php
<?php
namespace App\Facades;
class Greeting{
public function sayHello(){
return 'Hello Facades!';
}
}
MyFacade.php
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class MyFacade extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'myfacade'; }
}
Next, we can add the following code to our routes/web.php
:
app()->bind('myfacade',function (){
return new \App\Facades\Greeting();
});
use App\Facades\MyFacade;
Route::get('/', function () {
echo MyFacade::sayHello();
});
If we visit the homepage of our application, we'll see that the screen displays Hello Facades!. So, now you can see that we can call our class via our Facade interface, and we don't have to do this:
Route::get('/', function(){
$greeting = new App\Facades\Greeting;
$greeting->sayHello();
});
Instead of creating a new
instance of that object, we can instead use our Facade interface to call our methods.
Real-Time Facades
Real-Time Facades are even easier to use. You can add the word Facades
to the beginning of your namespace, and it will turn that class into a Facade. Here is a quick example.
Say that we have another blank Laravel application, and we created a new class at app\Greeting.php
with the following contents:
<?php
namespace App;
class Greeting{
public function sayHello(){
return 'Real-Time Facades are Amazing!';
}
}
Inside of our routes/web.php
, let's modify it to look like this:
use Facades\App\Greeting;
Route::get('/', function () {
echo Greeting::sayHello();
});
If we were to visit the homepage of our application, we would now get the message, "Real-Time Facades are Amazing!".
That's how simple it is to create a Real-Time Facade using Laravel. 😎
Conclusion
Real-Time Facades can come in handy, and they can make your code look a lot cleaner, so make sure to use them the next time you need to call a method statically 🤙.
Additionally, be sure to check out the Laravel documentation on Facades https://laravel.com/docs/facades.
Happy Coding! Talk to you later.