<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                * * * * * [TOC] ## 簡介 Facades(讀音:/f??s?d/ )為應用程序的?[服務容器](服務容器.md)?中可用的類提供了一個「靜態」接口。Laravel 自帶了很多 facades ,幾乎可以用來訪問到 Laravel 中所有的服務。Laravel facades 實際上是服務容器中那些底層類的「靜態代理」,相比于傳統的靜態方法, facades 在提供了簡潔且豐富的語法同時,還帶來了更好的可測試性和擴展性。 所有的 Laravel facades 都需要定義在命名空間?`Illuminate\Support\Facades`?下。所以,我們可以容易地向下面這樣調用 facade : ~~~ use Illuminate\Support\Facades\Cache; Route::get('/cache', function () { return Cache::get('key'); }); ~~~ 在 Laravel 的文檔中,很多示例代碼都是使用 facades 來演示框架的各種特性的。 ## 何時使用 Facades Facades 有很多好處,它為我們使用 Laravel 的各種功能提供了簡單,易記的語法,讓你不需要記住長長的類名來實現依賴注入和手動配置。還有,因為它們對于PHP動態方法的獨特用法,測試起來非常容易。 然而,在使用 facades 時,有些地方還需要特別注意。使用 facades 最主要的風險就是會引起類作用范圍的膨脹。因為 facades 使用起來非常簡單而且不需要注入,我們會不經意的在單個類中大量使用。它不會像使用依賴注入那樣,使用的類越多,構造方法會越長,在視覺上就會引起注意,提醒你這個類有點龐大了。所以在使用 facades 的時候,要特別注意控制好類的大小,讓類的作用范圍保持短小。 > {tip} 在開發與 Laravel 交互的第三方擴展包時,最好是在包中通過注入?[Laravel contracts](Contracts.md)?,而不是在包中通過 facades 來使用 Laravel 的類。因為擴展包不是在 Laravel 內部使用的,無法使用 Laravel's facade 的測試輔助函數。 ### Facades Vs. 依賴注入 依賴注入的一個主要的好處是可以切換注入類的具體實現。這在測試的時候很有用,因為你可以注入一個 mock 或者 stub ,并且對在 stub 中被調用的各種方法進行斷言。 通常,靜態方法是不可以被 mock 或者 stub 。但是,因為 facades 調用的是對象的動態方法,我們可以像測試注入類的實例一樣測試 facades ,例如,像下面的路由: ~~~ use Illuminate\Support\Facades\Cache; Route::get('/cache', function () { return Cache::get('key'); }); ~~~ 我們可以用下面的測試代碼去驗證?`Cache::get`?方法是否被調用,當傳入預期的參數時。 ~~~ use Illuminate\Support\Facades\Cache; /** * 一個基礎功能的測試用例。 * * @return void */ public function testBasicExample() { Cache::shouldReceive('get') ->with('key') ->andReturn('value'); $this->visit('/cache') ->see('value'); } ~~~ ### Facades Vs. 輔助函數 除了 facades , Laravel 包含一些「輔助函數」來實現一些常用的功能,比如生成視圖,觸發事件,調度任務或者發送 HTTP 響應。許多輔助函數的功能和對應的 facades 一樣。例如,下面這個 facade 和輔助函數的作用是一樣的: ~~~ return View::make('profile'); return view('profile'); ~~~ 這里的 facades 和輔助函數是沒有任何區別的。當你使用輔助函數時,你依然可以向使用對應的 facade 一樣測試他們。例如,下面的路由: ~~~ Route::get('/cache', function () { return cache('key'); }); ~~~ 在底層,輔助函數?`cache`?實際是調用?`Cache`?facade 中的?`get`?方法。因此,盡管我們是在使用輔助函數,我們依然可以用下面的測試代碼來驗證是否方法被正確調用,在傳入預期的參數時: ~~~ use Illuminate\Support\Facades\Cache; /** * 一個基礎功能的測試用例。 * * @return void */ public function testBasicExample() { Cache::shouldReceive('get') ->with('key') ->andReturn('value'); $this->visit('/cache') ->see('value'); } ~~~ ## Facades 工作原理 在 Laravel 應用中,一個 facade 就是一個提供訪問容器中對象的類。其中核心的部件就是?`Facade`?類。不管是 Laravel 自帶的 Facades ,還是用戶自定義的 Facades ,都繼承自?`Illuminate\Support\Facades\Facade`?類。 `Facade`?基類使用?`__callStatic()`?魔術方法在你的 facades 中延遲調用容器中對應對象的方法,在下面的例子中,調用了 Laravel 的緩存系統。在代碼里,我們可能認為是?`Cache`?類中的靜態方法?`get`?被調用了: ~~~ <?php namespace App\Http\Controllers; use Illuminate\Support\Facades\Cache; use App\Http\Controllers\Controller; class UserController extends Controller { /** * 顯示給定用戶的大體信息。 * * @param int $id * @return Response */ public function showProfile($id) { $user = Cache::get('user:'.$id); return view('profile', ['user' => $user]); } } ~~~ 注意在代碼的最上面,我們導入的是?`Cache`?facade 。這個 facade 其實是我們獲取底層?`Illuminate\Contracts\Cache\Factory`?接口實現的一個代理。我們通過這個 facade 調用的任何方法,都會被傳遞到 Laravel 緩存服務的底層實例中。 如果我們看一下?`Illuminate\Support\Facades\Cache`?這個類,你會發現類中根本沒有?`get`?這個靜態方法: ~~~ class Cache extends Facade { /** * 獲取組件在容器中注冊的名稱。 * * @return string */ protected static function getFacadeAccessor() { return 'cache'; } } ~~~ 其實,?`Cache`?facade 是繼承了?`Facade`?基類,并且定義了?`getFacadeAccessor()`?方法。這個方法的作用是返回服務容器中對應名字的綁定內容。當用戶調用?`Cache`?facade 中的任何靜態方法時, Laravel 會解析到服務容器中綁定的鍵值為?`cache`?實例對象,并調用這個對象對應的方法(在這個例子中就是?`get`?方法)。 ## Facade 類參考 在下面你可以找到每個 facade 類及其對應的底層類。這是一個查找給定 facade 類 API 文檔的有用工具。 也列出了綁定在?[服務容器](https://laravel-china.org/docs/laravel/5.4/container)?中 facade 類對應的可用鍵值。 | Facade | Class | Service Container Binding | | --- | --- | --- | | App | [Illuminate\Foundation\Application](https://laravel.com/api/laravel/5.4/Illuminate/Foundation/Application.html) | `app` | | Artisan | [Illuminate\Contracts\Console\Kernel](https://laravel.com/api/laravel/5.4/Illuminate/Contracts/Console/Kernel.html) | `artisan` | | Auth | [Illuminate\Auth\AuthManager](https://laravel.com/api/laravel/5.4/Illuminate/Auth/AuthManager.html) | `auth` | | Blade | [Illuminate\View\Compilers\BladeCompiler](https://laravel.com/api/laravel/5.4/Illuminate/View/Compilers/BladeCompiler.html) | `blade.compiler` | | Bus | [Illuminate\Contracts\Bus\Dispatcher](https://laravel.com/api/laravel/5.4/Illuminate/Contracts/Bus/Dispatcher.html) | ? | | Cache | [Illuminate\Cache\Repository](https://laravel.com/api/laravel/5.4/Illuminate/Cache/Repository.html) | `cache` | | Config | [Illuminate\Config\Repository](https://laravel.com/api/laravel/5.4/Illuminate/Config/Repository.html) | `config` | | Cookie | [Illuminate\Cookie\CookieJar](https://laravel.com/api/laravel/5.4/Illuminate/Cookie/CookieJar.html) | `cookie` | | Crypt | [Illuminate\Encryption\Encrypter](https://laravel.com/api/laravel/5.4/Illuminate/Encryption/Encrypter.html) | `encrypter` | | DB | [Illuminate\Database\DatabaseManager](https://laravel.com/api/laravel/5.4/Illuminate/Database/DatabaseManager.html) | `db` | | DB (Instance) | [Illuminate\Database\Connection](https://laravel.com/api/laravel/5.4/Illuminate/Database/Connection.html) | ? | | Event | [Illuminate\Events\Dispatcher](https://laravel.com/api/laravel/5.4/Illuminate/Events/Dispatcher.html) | `events` | | File | [Illuminate\Filesystem\Filesystem](https://laravel.com/api/laravel/5.4/Illuminate/Filesystem/Filesystem.html) | `files` | | Gate | [Illuminate\Contracts\Auth\Access\Gate](https://laravel.com/api/laravel/5.4/Illuminate/Contracts/Auth/Access/Gate.html) | ? | | Hash | [Illuminate\Contracts\Hashing\Hasher](https://laravel.com/api/laravel/5.4/Illuminate/Contracts/Hashing/Hasher.html) | `hash` | | Lang | [Illuminate\Translation\Translator](https://laravel.com/api/laravel/5.4/Illuminate/Translation/Translator.html) | `translator` | | Log | [Illuminate\Log\Writer](https://laravel.com/api/laravel/5.4/Illuminate/Log/Writer.html) | `log` | | Mail | [Illuminate\Mail\Mailer](https://laravel.com/api/laravel/5.4/Illuminate/Mail/Mailer.html) | `mailer` | | Notification | [Illuminate\Notifications\ChannelManager](https://laravel.com/api/laravel/5.4/Illuminate/Notifications/ChannelManager.html) | ? | | Password | [Illuminate\Auth\Passwords\PasswordBrokerManager](https://laravel.com/api/laravel/5.4/Illuminate/Auth/Passwords/PasswordBrokerManager.html) | `auth.password` | | Queue | [Illuminate\Queue\QueueManager](https://laravel.com/api/laravel/5.4/Illuminate/Queue/QueueManager.html) | `queue` | | Queue (Instance) | [Illuminate\Contracts\Queue\Queue](https://laravel.com/api/laravel/5.4/Illuminate/Contracts/Queue/Queue.html) | `queue` | | Queue (Base Class) | [Illuminate\Queue\Queue](https://laravel.com/api/laravel/5.4/Illuminate/Queue/Queue.html) | ? | | Redirect | [Illuminate\Routing\Redirector](https://laravel.com/api/laravel/5.4/Illuminate/Routing/Redirector.html) | `redirect` | | Redis | [Illuminate\Redis\Database](https://laravel.com/api/laravel/5.4/Illuminate/Redis/Database.html) | `redis` | | Request | [Illuminate\Http\Request](https://laravel.com/api/laravel/5.4/Illuminate/Http/Request.html) | `request` | | Response | [Illuminate\Contracts\Routing\ResponseFactory](https://laravel.com/api/laravel/5.4/Illuminate/Contracts/Routing/ResponseFactory.html) | ? | | Route | [Illuminate\Routing\Router](https://laravel.com/api/laravel/5.4/Illuminate/Routing/Router.html) | `router` | | Schema | [Illuminate\Database\Schema\Blueprint](https://laravel.com/api/laravel/5.4/Illuminate/Database/Schema/Blueprint.html) | ? | | Session | [Illuminate\Session\SessionManager](https://laravel.com/api/laravel/5.4/Illuminate/Session/SessionManager.html) | `session` | | Session (Instance) | [Illuminate\Session\Store](https://laravel.com/api/laravel/5.4/Illuminate/Session/Store.html) | ? | | Storage | [Illuminate\Contracts\Filesystem\Factory](https://laravel.com/api/laravel/5.4/Illuminate/Contracts/Filesystem/Factory.html) | `filesystem` | | URL | [Illuminate\Routing\UrlGenerator](https://laravel.com/api/laravel/5.4/Illuminate/Routing/UrlGenerator.html) | `url` | | Validator | [Illuminate\Validation\Factory](https://laravel.com/api/laravel/5.4/Illuminate/Validation/Factory.html) | `validator` | | Validator (Instance) | [Illuminate\Validation\Validator](https://laravel.com/api/laravel/5.4/Illuminate/Validation/Validator.html) | ? | | View | [Illuminate\View\Factory](https://laravel.com/api/laravel/5.4/Illuminate/View/Factory.html) | `view` | | View (Instance) | [Illuminate\View\View](https://laravel.com/api/laravel/5.4/Illuminate/View/View.html) | ? |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看