Info: Bài viết được lấy từ nguồn https://github.com/petehouston/laravel-docs-vn

Facades

Giới thiệu

Facades cung cấp một interface "tĩnh" cho các class sử dụng trong service container của ứng dụng. Laravel cung cấp sẵn nhiều facade, và bạn có thể đã sử dụng chúng mà không hề biết! Laravel "facade" phục vụ như "proxies tĩnh" cho các class bên dưới ở trong service container, cung cấp lợi ích của việc sử dụng cú pháp vừa ngắn gọn vừa có thể bảo trì có thể thoải mái hơn là sử dụng các phương thức tĩnh truyền thống.

Sử dụng Facades

Trong một ứng dụng Laravel, facade là một class cung cấp truy cập vào một đối tượng từ container. Bộ máy làm cho cách này hoạt động được là nhờ vào class Facade. Các facade của Laravel, và bất cứ facade mà bạn tạo ra đều được kế thừa từ class cơ sở Illuminate\Support\Facades\Facade.

Một class facade chỉ cần triển khai một phương thức duy nhất: getFacadeAccessor. Việc của getFacadeAccessor là định nghĩa cái gì cần được resolve từ container. Class Facade cơ sở tận dụng phương thức magic __callStatic() để điều việc thực thi từ facade tới object đã được resolve.

Trong ví dụ dưới đây, một lệnh thực thi được gọi tới hệ thống cache của Laravel. Nhìn qua đoạn code này, bạn có thể nghĩ là phương thức tĩnh get đang được gọi trong class Cache:

<?php

namespace App\Http\Controllers;

use Cache;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    public function showProfile($id)
    {
        $user = Cache::get('user:'.$id);

        return view('profile', ['user' => $user]);
    }
}

Chú ý ở gần đầu file chúng ta có thực hiện "importing" vào facade Cache. Facade này đóng vai trò như một proxy để truy cập vào phần triển khai phía dưới của interface Illuminate\Contracts\Cache\Factory. Bất cứ việc gọi nào mà chúng ta sử dụng từ facade sẽ được đẩy tới instance phía dưới của Laravel cache service.

Nếu nhìn vào class Illuminate\Support\Facades\Cache, bạn sẽ thấy không hề có phương thức tĩnh get nào cả:

class Cache extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor() { return 'cache'; }
}

Thay vào đó, facade Cache mở rộng class cơ sở Facade và định nghĩa phương thức getFacadeAccessor(). Nhớ là nhiệm vụ của phương thức này là trả về tên của liên kết trong service container. Khi mà người dùng tham chiếu tới bất kì phương thức tĩnh nào trong facade Cache, Laravel thực hiện việc Resolve liên kết cache từ service container và thực thi phương thức được gọi (trong trường hợp này, get) đối với object.

Tham khảo các Facade

Dưới đây, bạn có thể tìm các facade và lớp dưới của nó, rất hữu ích khi bạn muốn tìm hiểu nhanh tài liệu API cho một facade nào đó. Các tên khoá liên kết vào trong service container cũng được ghi kèm theo nếu có.

Tên Facade | Tên Class | Service Container Binding ------------- | ------------- | ------------- App | Illuminate\Foundation\Application | app Artisan | Illuminate\Contracts\Console\Kernel | artisan Auth | Illuminate\Auth\AuthManager | auth Blade | Illuminate\View\Compilers\BladeCompiler | blade.compiler Bus | Illuminate\Contracts\Bus\Dispatcher | Cache | Illuminate\Cache\Repository | cache 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 | 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\Writer | log Mail | Illuminate\Mail\Mailer | mailer Password | Illuminate\Auth\Passwords\PasswordBroker | auth.password Queue | Illuminate\Queue\QueueManager | queue Queue (Instance) | Illuminate\Contracts\Queue\Queue | queue Queue (Base Class) | Illuminate\Queue\Queue | Redirect | Illuminate\Routing\Redirector | redirect Redis | Illuminate\Redis\Database | redis Request | Illuminate\Http\Request | request Response | Illuminate\Contracts\Routing\ResponseFactory | Route | Illuminate\Routing\Router | router Schema | Illuminate\Database\Schema\Blueprint | Session | Illuminate\Session\SessionManager | session Session (Instance) | Illuminate\Session\Store | Storage | Illuminate\Contracts\Filesystem\Factory | filesystem 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 |