Laravel – Facades

Facades cung cấp phương thức “static” để tương tác với các class được khai báo bên trong service container. Laravel “facade” service giống như “static proxies” cho các class bên dưới trong service container. Nó giúp cú pháp ngắn ngọn, dễ hiểu hơn so với phương thức static truyền thống.

Ví dụ : Auth::check(); hay Route::get(); là những Facade.

Tạo một Facade

Các bước thực hiện để tạo 1 Facade.

Step 1 : Tạo service provider.

Step 2 : Tạo file PHP Class.

Step 3 : Bind class đó vào Service Provider ở step 1.

Step 4 : Tạo Class Facade và kế thừa từ lluminate\Support\Facades\Facade.

Step 5 : Đăng ký ServiceProvider (Step 3), Facade(Step 4) vào file Config\app.php.

Tham khảo Facade Class

Laravel có nhiều Facades, dưới đây là những Facade được xây dựng sẵn trong laravel 5.6.

Facade Class Service Container Binding
App Illuminate\Foundation\Application app
Artisan Illuminate\Contracts\Console\Kernel artisan
Auth Illuminate\Auth\AuthManager auth
Auth (Instance) Illuminate\Contracts\Auth\Guard auth.driver
Blade Illuminate\View\Compilers\BladeCompiler blade.compiler
Broadcast Illuminate\Contracts\Broadcasting\Factory
Broadcast (Instance) Illuminate\Contracts\Broadcasting\Broadcaster
Bus Illuminate\Contracts\Bus\Dispatcher
Cache Illuminate\Cache\CacheManager cache
Cache (Instance) Illuminate\Cache\Repository cache.store
Config Illuminate\Config\Repository config
Cookie Illuminate\Cookie\CookieJar cookie
Crypt Illuminate\Encryption\Encrypter encrypter
DB Illuminate\Database\DatabaseManager db
DB (Instance) Illuminate\Database\Connection db.connection
Event Illuminate\Events\Dispatcher events
File Illuminate\Filesystem\Filesystem files
Gate Illuminate\Contracts\Auth\Access\Gate
Hash Illuminate\Contracts\Hashing\Hasher hash
Lang Illuminate\Translation\Translator translator
Log Illuminate\Log\Logger log
Mail Illuminate\Mail\Mailer mailer
Notification Illuminate\Notifications\ChannelManager
Password Illuminate\Auth\Passwords\PasswordBrokerManager auth.password
Password (Instance) Illuminate\Auth\Passwords\PasswordBroker auth.password.broker
Queue Illuminate\Queue\QueueManager queue
Queue (Instance) Illuminate\Contracts\Queue\Queue queue.connection
Queue (Base Class) Illuminate\Queue\Queue
Redirect Illuminate\Routing\Redirector redirect
Redis Illuminate\Redis\RedisManager redis
Redis (Instance) Illuminate\Redis\Connections\Connection redis.connection
Request Illuminate\Http\Request request
Response Illuminate\Contracts\Routing\ResponseFactory
Response (Instance) Illuminate\Http\Response
Route Illuminate\Routing\Router router
Schema Illuminate\Database\Schema\Builder
Session Illuminate\Session\SessionManager session
Session (Instance) Illuminate\Session\Store session.store
Storage Illuminate\Filesystem\FilesystemManager filesystem
Storage (Instance) Illuminate\Contracts\Filesystem\Filesystem filesystem.disk
URL Illuminate\Routing\UrlGenerator url
Validator Illuminate\Validation\Factory validator
Validator (Instance) Illuminate\Validation\Validator
View Illuminate\View\Factory view
View (Instance) Illuminate\View\View

Ví dụ

Step 1 : Tạo một service provider tên là TestFacadesServiceProvider bằng lệnh command bên dưới:

php artisan make:provider TestFacadesServiceProvider

Khi thực hiện thành công, sẽ có thông báo sau:

Step 2 : Tạo file TestFacades.php trong thư mục App/Test.

App/Test/TestFacades.php

<?php
namespace App\Test;

class TestFacades{
   public function testingFacades(){
      echo "Testing the Facades in Laravel.";
   }
}
?>

step 3 : mở file App/Providers/TestFacadesServiceProvider.php đã tạo ở step 1, thêm:

use App;

App::bind('test',function() {
	return new \App\Test\TestFacades;
});

Step 4 : Tạo class Facade TestFacades.php trong App/Test/Facades.
App/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 : Thêm vào file config/app.php.
Thêm service provider

'providers' => [
...
App\Providers\TestFacadesServiceProvider::class,
...
],

Thêm alias

'aliases' => [
    ...
    'TestFacades' => App\Test\Facades\TestFacades::class,
    ...
],

Step 6 : Thêm route bên dưới vào file route\web.php.

Route::get('/facadeex', function(){
   return TestFacades::testingFacades();
});

Step 7 : Vào đường dẫn bên dưới để kiểm tra kết quả Facade.

http://localhost/laravel/facadeex