上一篇php
One of the keys to building a well architected Laravel application is learning to use service providers as an organizational tool. When you are registering many classes with the IoC container, all of those bindings can start to clutter your app/start
files. Instead of doing container registerations in those files, create service providers that register related services.數據庫
想製做一個結構優美的Laravel應用的話,就要去學習如何用服務提供者來管理代碼。當你在註冊IoC綁定的時候,全部代碼都雜亂的塞進了app/start
路徑下的文件裏。 別再這樣作了,使用服務提供者來註冊這些吧。bootstrap
Get It Started 萬物之初
Your application's "start" files are all stored in the
app/start
directory. These are "bootstrap" files that are loaded based on the type of the request entering your application. Start files named after an environment are loaded after the globalstart.php
file. Finally, theartisan.php
start file is loaded when any console command is executed.數組你應用的「啓動」文件都儲存在
app/start
目錄下。根據不一樣的請求入口,系統會載入不一樣的啓動文件。在全局的start.php
文件加載後,系統會根據執行環境的不一樣來加載不一樣的啓動文件。 此外,在執行命令行程序時,artisan.php
文件會被載入。app
Let's explore an example. Perhaps our application is using Pusher to push messages to clients via WebSockets. In order to decouple ourselves from Pusher, it will be benificial to create an EventPusherInterface
, and a PusherEventPusher
implementation. This will allow us to easily change WebSocket providers down the road as requirements change or our application grows.ide
我們來考慮這個例子。也許咱們的應用正在使用Pusher 來爲客戶推送消息。爲了將咱們的應用和Pusher解耦,咱們要定義EventPusherInterface
接口和對應的實現類PusherEventPusher
。這樣在需求變化或應用改進時,咱們就能夠隨時輕鬆的改變推送服務提供商。工具
<!-- lang: php --> interface EventPusherInterface{ public function push($message, array $data = array()); } class PusherEventPusher implements EventPusherInterface{ public function __construct(PusherSdk $pusher) { $this->pusher = $pusher; } public function push($message, array $data = array()) { // Push message via the Pusher SDK... } }
Next, let's create an EventPusherServiceProvider
:學習
接下來咱們建立一個EventPusherServiceProvider
:ui
<!-- lang:php --> use Illuminate\Support\ServiceProvider; class EventPusherServiceProvider extends ServiceProvider { public function register() { $this->app->singleton('PusherSdk', function() { return new PusherSdk('app-key', 'secret-key'); } $this->app->singleton('EventPusherInterface', 'PusherEventPusher'); } }
Great! Now we have a clean abstraction over event pushing, as well as a convenient place to register this, and other related bindings, in the container. Finally, we just need to add the EventPusherServiceProvider
to our providers
array in the app/config/app.php
configuration file. Now we are ready to inject the EventPusherInterface
into any controller or class within our application.this
很好! 咱們對事件推送進行了清晰的抽象,同時咱們也有了一個很不錯的地方進行註冊、綁定其餘相關的東西到容器裏。最後一步只須要將EventPusherServiceProvider
寫入app/config/app.php
文件內的providers
數組裏就能夠了。如今這個應用裏的EventPusherInterface
已經被綁定到了正確的實現類上。
Should You Singleton? 要使用單例麼?
You will need to evaluate whether to bind classes with
bind
orsingleton
. If you only want one instance of the class to be created per request cycle, usesingleton
. Otherwise, usebind
.用不用單例能夠這樣來考慮:若是在一次請求週期中該類只須要有一個實例,就使用
singleton
;不然就使用bind
。
Note that a service provider has an $app
instance available via the base ServiceProvider
class. This is a full Illuminate\Foundation\Application
instance, which inherits from the Container
class, so we can call all of the IoC container methods we are used to. If you preffer to use the App
facade inside the service provider, you may do that as well:
App::singleton('EventPusherInterface', 'PusherEventPusher');
Of course, service providers are not limited to registering certain kinds of services. We could use them to register our cloud file storage services, database access services, a custom view engine such as Twig, etc. They are simply bootstrapping and organizational tools for your application. Nothing more.
固然服務提供者的功能不單單侷限於消息推送。像是雲存儲、數據庫訪問、自定義的視圖引擎好比Twig等等均可以用這種模式來設置。服務提供者就是你的應用裏的啓動代碼和管理工具,沒什麼神奇的。
So, don't be scared to create your own service providers. They are not something that should be strictly limited to distributed packages, but rather are great organizational tools for your own applications. Be creative and use them to bootstrap your various application components.
因此大膽的去建立你本身的服務提供者。並非你非要發佈個什麼軟件包才須要服務提供者,他們只是很是好的管理代碼的工具。使用它們的力量去管理好應用中的各個組件吧。