<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 服務提供器 - [簡介](#introduction) - [編寫服務提供器](#writing-service-providers) - [注冊方法](#the-register-method) - [引導方法](#the-boot-method) - [注冊提供器](#registering-providers) - [延遲提供器](#deferred-providers) <a name="introduction"></a> ## 簡介 服務提供器是所有 Laravel 應用程序引導中心。你的應用程序以及 Laravel 的所有核心服務都是通過服務提供器進行引導。 在這里,我們說的「引導」其實是指**注冊**,比如注冊服務容器綁定、事件監聽器、中間件,甚至是路由的注冊。服務提供器是配置你的應用程序的中心。 Laravel 的 `config/app.php` 文件中有一個 `providers` 數組。數組中的內容是應用程序要加載的所有服務提供器類。這其中有許多提供器并不會在每次請求時都被加載,只有當它們提供的服務實際需要時才會加載。這種我們稱之為「延遲」提供器。 本篇將帶你學習如何編寫自己的服務提供器,并將其注冊到 Laravel 應用程序中。 <a name="writing-service-providers"></a> ## 編寫服務提供器 所有服務提供器都會繼承 `Illuminate\Support\ServiceProvider` 類。大多數服務提供器都包含 `register` 和 `boot` 方法。在 `register` 方法中,你**只需要綁定類到 [服務容器](/docs/{{version}}/container)**中。而不需要嘗試在 `register` 方法中注冊任何事件監聽器、路由或任何其他功能。 使用 Artisan 命令行界面,通過 `make:provider` 命令生成一個新的提供器: php artisan make:provider RiakServiceProvider <a name="the-register-method"></a> ### 注冊方法 在 `register` 方法中,你只需要將類綁定到 [服務容器](/docs/{{version}}/container) 中。而不需要嘗試在 `register` 方法中注冊任何事件監聽器、路由或者任何其他功能。否則,你可能會意外使用到尚未加載的服務提供器提供的服務。 讓我們來看一個基本的服務提供器。在你的任何服務提供器方法中,你可以通過 `$app` 屬性來訪問服務容器: <?php namespace App\Providers; use Riak\Connection; use Illuminate\Support\ServiceProvider; class RiakServiceProvider extends ServiceProvider { /** * 在容器中注冊綁定。 * * @return void */ public function register() { $this->app->singleton(Connection::class, function ($app) { return new Connection(config('riak')); }); } } 這個服務提供器只定義了一個 `register` 方法,并使用該方法在服務容器中定義了一個 `Riak\Connection` 實現。 如果你不了解服務容器的工作原理,請查看其 [文檔](/docs/{{version}}/container). <a name="the-boot-method"></a> ### 引導方法 那么,如果我們需要在我們的服務提供器中注冊一個視圖組件呢?這應該在 `boot` 方法中完成。**此方法在所有其他服務提供器都注冊之后才能調用**,這意味著你可以訪問已經被框架注冊的所有服務: <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class ComposerServiceProvider extends ServiceProvider { /** * 引導任何應用程序服務。 * * @return void */ public function boot() { view()->composer('view', function () { // }); } } #### 引導方法依賴注入 你可以為服務提供器的 `boot` 方法設置類型提示。[服務容器](/docs/{{version}}/container) 會自動注入你需要的任何依賴項: use Illuminate\Contracts\Routing\ResponseFactory; public function boot(ResponseFactory $response) { $response->macro('caps', function ($value) { // }); } <a name="registering-providers"></a> ## 注冊提供器 所有服務提供器都在 `config/app.php` 配置文件中注冊。該文件中有一個 `providers` 數組,用于存放服務提供器的類名 。默認情況下,這個數組列出了一系列 Laravel 核心服務提供器。這些服務提供器引導 Laravel 核心組件,例如郵件、隊列、緩存等。 要注冊提供器,只需要將其添加到數組: 'providers' => [ // 其他服務提供器 App\Providers\ComposerServiceProvider::class, ], <a name="deferred-providers"></a> ## 延遲提供器 如果你的提供器**僅**在 [服務容器](/docs/{{version}}/container) 中注冊綁定,就可以選擇推遲其注冊,直到當它真正需要注冊綁定。推遲加載這種提供器會提高應用程序的性能,因為它不會在每次請求時都從文件系統中加載。 Laravel 編譯并保存延遲服務提供器提供的所有服務的列表,以及其服務提供器類的名稱。因此,只有當你在嘗試解析其中一項服務時,Laravel 才會加載服務提供器。 要延遲提供器的加載,請將 `defer` 屬性設置為 `true` ,并定義 `provides` 方法。`provides` 方法應該返回由提供器注冊的服務容器綁定: <?php namespace App\Providers; use Riak\Connection; use Illuminate\Support\ServiceProvider; class RiakServiceProvider extends ServiceProvider { /** * 是否延時加載提供器。 * * @var bool */ protected $defer = true; /** * 注冊服務提供器。 * * @return void */ public function register() { $this->app->singleton(Connection::class, function ($app) { return new Connection($app['config']['riak']); }); } /** * 獲取提供器提供的服務。 * * @return array */ public function provides() { return [Connection::class]; } } ## 譯者署名 | 用戶名 | 頭像 | 職能 | 簽名 | |---|---|---|---| | [@devindwan](https://github.com/devindwan) | <img class="avatar-66 rm-style" src="https://avatars2.githubusercontent.com/u/10205466?v=4&s=100"> | 翻譯 | There are only two kinds of languages: the ones people complain about and the ones nobody uses. | | [@JokerLinly](https://laravel-china.org/users/5350) | <img class="avatar-66 rm-style" src="https://dn-phphub.qbox.me/uploads/avatars/5350_1481857380.jpg"> | Review | Stay Hungry. Stay Foolish. | --- > {note} 歡迎任何形式的轉載,但請務必注明出處,尊重他人勞動共創開源社區。 > > 轉載請注明:本文檔由 Laravel China 社區 [laravel-china.org](https://laravel-china.org) 組織翻譯,詳見 [翻譯召集帖](https://laravel-china.org/topics/5756/laravel-55-document-translation-call-come-and-join-the-translation)。 > > 文檔永久地址: https://d.laravel-china.org
                  <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>

                              哎呀哎呀视频在线观看