Laravel - Facades

Facades provide a static interface to classes that are available in the application's service container. Laravel facades serve as static proxy for base classes in the service container, providing the benefit of a concise, expressive syntax while retaining more testability and flexibility than traditional static methods.
How to create a facade
Following are the steps to create Facade in Laravel −
-
Step 1 - Create a PHP class file.
-
Step 2 − Bind this class to the service provider.
-
Step 3 - Register this ServiceProvider for
Config\app.php as providers.
-
Step 4 - Create a class that extends to this class
lluminate \ Support \ Facades \ Facade.
-
Step 5 - Register item 4 in Config\app.php as aliases.
Step 1 - Create a PHP class file.
Step 2 − Bind this class to the service provider.
Step 3 - Register this ServiceProvider for
Config\app.php as providers.
Step 4 - Create a class that extends to this class
lluminate \ Support \ Facades \ Facade.
Step 5 - Register item 4 in Config\app.php as aliases.
Facade class reference
Laravel comes with many facades. The following table shows the built-in references to the Facade class −
Facade | Class | Service container binding |
---|---|---|
Appendix | Illuminate\Foundation\Application | Appendix |
craftsman | Illuminate\Contracts\Console\Kernel | craftsman |
Auth | Illuminate \Auth\AuthManager | auth |
Auth (instance) | Illuminate\Auth\Guard | |
blade | Illuminate\View\Compilers\BladeCompiler | blade.compiler |
bus | Illuminate\Contracts\Bus\Dispatcher | |
cache | Illuminate \Cache\Repository | cache |
config | Illuminate\Config\Repository | config |
cookies | Illuminate\Cookie\CookieJar | cookies |
crypt | Illuminate\Encryption\Encrypter | encrypter |
database | Illuminate \Database\DatabaseManager | decibel |
DB (Instance) | Illuminate \Database\Connection | |
Event | Illuminate \ Events \ Shipper | Events |
file | Illuminate\FileSystem\Filesystem | files |
Gates | Illuminate\Contracts\Auth\Access\Gate | |
hashish | Illuminate \ Contracts \ Hashing \ Hasher | hashish |
entrance | Illuminate\Http\Request | inquiry |
Lang | Illuminate \ Translation \ Translator | translator |
Magazine | Illuminate\Log\Writer | magazine |
Illuminate\Mail\Mailer | mailing list | |
password | Illuminate \Auth\Passwords\PasswordBroker | auth.password |
Queue | Illuminate\Queue\QueueManager | queue |
Queue (Instance) | Illuminate\Queue\QueueInterface | |
Queue (Base class) | Illuminate\Queue\Queue | |
redirect | Illuminate \ Routing \ Redirector | redirect |
Redis | Illuminate\Redis\Database | Redis |
Inquiry | Illuminate\Http\Request | inquiry |
response | Illuminate\Contracts\Routing\ResponseFactory | |
route | Illuminate\Routing\Router | router |
scheme | Illuminate\Database\Schemes\Blueprint | |
session | Illuminate\Session\SessionManager | session |
Session (Instance) | Illuminate\Session\shop | |
Storage | Illuminate\Contracts\Filesystem\Factory | file system |
URL | Illuminate\Routing\UrlGenerator | URL |
Validator | Illuminate\Validation\Factory | validator |
Validator (Instance) | Illuminate\Validation\Validator | |
Look | Illuminate\View\Factory | Look |
View (Instance) | Illuminate\View\View |
example
Step 1: Create a service provider named TestFacadesServiceProvider by running the following command.
php artisan make:provider TestFacadesServiceProvider
Step 2 − Upon successful execution, you will get the following output −
Step 3 − Create a class named TestFacades.php in App/Test .
Application/Test/TestFacades.php
<? php namespace App \Test ; class TestFacades { public function testingFacades () { echo "Testing the Facades in Laravel." ; } } ?>
Step 4 − Create a Facade class named "TestFacades.php" in "App/Test/Facades" .
Application/Test/Facades/TestFacades.php
<?php namespace app\Test\Facades; use Illuminate\Support\Facades\Facade; class TestFacades extends Facade { protected static function getFacadeAccessor() { return 'test'; } }
Step 5 − Create Facade class named TestFacadesServiceProviders.php in App/Test/Facades.
App/Providers/TestFacadesServiceProviders.php
<?php namespace App\Providers; use App; use Illuminate\Support\ServiceProvider; class TestFacadesServiceProvider extends ServiceProvider { public function boot() { // } public function register() { App::bind('test',function() { return new \App\Test\TestFacades; }); } }
Step 6 − Add Service Provider in config/app.php file as shown in the image below.
config/app.php
Step 7 − Add an alias to the config/app.php file as shown in the image below.
config/app.php
Step 8 - Add the following lines to app/Http/rout.php.
app/http/routes.php
Route::get('/facadeex', function() { return TestFacades::testingFacades(); });
Step 9 − Visit the following URL to check Facade.
http://localhost:8000/facadeex
Step 10 − After visiting the URL, you will get the following output −