Facades in Laravel — How to create and use custom facades in Laravel 7/8/9
--
Facades were one of the things that took me time to grasp as a Laravel developer. I wanted to understand what facades were and how they work. So I’m writing this to help anyone who wants to know how facades work in Laravel, how to create and use custom facades and why to use them too.
So what is a Facade?
A facade in Laravel is a wrapper around a non-static function that turns it into a static function. Facades provide a static interface to classes that are available in the application’s service container. Laravel facades serve as static proxies to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.
Don’t let the technical jargon blow you off, let’s look at it this way; what are facades in real life?
“A facade is the front of a building, or a kind of front people put up emotionally. If you’re mad but acting happy, you’re putting up a facade. This word has to do with the outer layer of something. One sense has to do with the front or outside of a building.” — (vocabulary.com)
Now you get sense of what it means in Laravel?
A facade in Laravel is a front (A simple interface) to a more complex non-static class, it makes using that class look smarter and neat, it’s a syntactic sugar.
How to create Facade
The following are the steps to create Facade in Laravel −
- Create PHP Class File.
- Bind that class to Service Provider.
- Register that ServiceProvider to Config\app.php as providers.
- Create Class which is this class extends to lluminate\Support\Facades\Facade.
- Register point 4 to Config\app.php as aliases.
Example: We will create a Facade for Paint.
Step 1 − Create a service provider called PaintFacadesServiceProvider by executing the following command.
php artisan make:provider PaintFacadesServiceProvider
Step 2− Create a class called PaintFacades.php at App/Paint.
App/Paint/PaintFacades.php
<?php
namespace App\Paint;
class PaintFacades{
public function color() {
echo "This can bring a list of all colors we need";
}
public function splash() {
echo "Splash the color on the wall, it's fun";
}
public function screed() {
echo "This is a pre-painting routine to make the wall smooth";
}
}
?>
Step 3 − Create a Facade class called “PaintFacades.php” at “App/Paint/Facades”.
App/Paint/Facades/PaintFacades.php
<?php
namespace app\Paint\Facades;
use Illuminate\Support\Facades\Facade;
class PaintFacades extends Facade {
protected static function getFacadeAccessor() { return 'paint'; }
}
Step 4 − Open the Facade class called PaintFacadesServiceProviders.php at App/Providers created in step 1.
App/Providers/PaintFacadesServiceProviders.php
<?php
namespace App\Providers;
use App;
use Illuminate\Support\ServiceProvider;
class PaintFacadesServiceProvider extends ServiceProvider {
public function boot() {
//
}
public function register() {
App::bind('paint',function() {
return new \App\Paint\PaintFacades;
});
}
}
Step 5− Add a service provider in a file config/app.php as shown below.
......
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
//Add your Service provider here
App\Providers\PaintFacadesServiceProvider::class,
Step 6− Add an alias in a file config/app.php as shown below.
/*
* Application Service Providers...
*/
'Storage' => Illuminate\Support\Facades\Storage::class,
'Str' => Illuminate\Support\Str::class,
'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
//Add your alias here
'Paint' => App\Paint\Facades\PaintFacades::class,
What next?
Nothing! We can test our Facade to see if it’s working. Add the following lines in routes/web.php.
Route::get('/color', function() {
return Paint::color();
});
Route::get('/splash', function() {
return Paint::splash();
});
Route::get('/whatever', function() {
return Paint::screed();
});
Start your server
php artisan serve
Visit any of the following URLs to test the Facade.
http://localhost:8000/color
http://localhost:8000/splash
http://localhost:8000/whatever
On your browser, you’ll get these as you run the urls respectively
This can bring a list of all colors we need
Splash the color on the wall, it's fun
This is a pre-painting routine to make the wall smooth
That’s it, we can now use our Facade in any part of our application. Creating large applications can be complex but here we try to make each part KISS simple, You can always reach out to the Facades to get the full documentation. Try it out and use it in your next project.
Stay tuned!!! I will be back with some more cool Laravel tutorials in the next article. I hope you liked the article. Don’t forget to follow me 😇 and give some clap 👏. And if you have any questions feel free to comment.
Thank you 🙏
Thanks a lot for reading till end 🙏 You can contact me in case if you need any assistance:
Email: prevailexcellent@gmail.com
Github: https://github.com/PrevailExcel
LinkedIn: https://www.linkedin.com/in/chimeremeze-prevail-ejimadu-3a3535219
Twitter: https://twitter.com/EjimaduPrevail