綁定基礎(chǔ)
幾乎所有的服務(wù)容器綁定都是在 服務(wù)提供者 中完成。
在目錄結(jié)構(gòu)如下圖

注:如果一個(gè)類沒有基于任何接口那么就沒有必要將其綁定到容器。容器并不需要被告知如何構(gòu)建對(duì)象,因?yàn)樗鼤?huì)使用 PHP 的反射服務(wù)自動(dòng)解析出具體的對(duì)象。
簡(jiǎn)單的綁定
在一個(gè)服務(wù)提供者中,可以通過 $this->app 變量訪問容器,然后使用 bind 方法注冊(cè)一個(gè)綁定,該方法需要兩個(gè)參數(shù),第一個(gè)參數(shù)是我們想要注冊(cè)的類名或接口名稱,第二個(gè)參數(shù)是返回類的實(shí)例的閉包:
$this->app->bind('HelpSpot\API', function ($app) {
return new HelpSpot\API($app->make('HttpClient'));
});
注意到我們將容器本身作為解析器的一個(gè)參數(shù),然后我們可以使用該容器來解析我們正在構(gòu)建的對(duì)象的子依賴。
綁定一個(gè)單例
singleton 方法綁定一個(gè)只會(huì)解析一次的類或接口到容器,然后接下來對(duì)容器的調(diào)用將會(huì)返回同一個(gè)對(duì)象實(shí)例:
$this->app->singleton('HelpSpot\API', function ($app) {
return new HelpSpot\API($app->make('HttpClient'));
});
綁定原始值
你可能有一個(gè)接收注入類的類,同時(shí)需要注入一個(gè)原生的數(shù)值比如整型,可以結(jié)合上下文輕松注入這個(gè)類需要的任何值:
$this->app->when('App\Http\Controllers\UserController')
->needs('$variableName')
->give($value);
綁定接口到實(shí)現(xiàn)
服務(wù)容器的一個(gè)非常強(qiáng)大的功能是其綁定接口到實(shí)現(xiàn)。我們假設(shè)有一個(gè) EventPusher 接口及其實(shí)現(xiàn)類 RedisEventPusher ,編寫完該接口的 RedisEventPusher 實(shí)現(xiàn)后,就可以將其注冊(cè)到服務(wù)容器:
$this->app->bind(
'App\Contracts\EventPusher',
'App\Services\RedisEventPusher'
);
這段代碼告訴容器當(dāng)一個(gè)類需要 EventPusher 的實(shí)現(xiàn)時(shí)將會(huì)注入 RedisEventPusher,現(xiàn)在我們可以在構(gòu)造器或者任何其它通過服務(wù)容器注入依賴的地方進(jìn)行 EventPusher 接口的依賴注入:
use App\Contracts\EventPusher;
/**
* 創(chuàng)建一個(gè)新的類實(shí)例
*
* @param EventPusher $pusher
* @return void
*/
public function __construct(EventPusher $pusher){
$this->pusher = $pusher;
}
上下文綁定
有時(shí)侯我們可能有兩個(gè)類使用同一個(gè)接口,但我們希望在每個(gè)類中注入不同實(shí)現(xiàn),例如,兩個(gè)控制器依賴 Illuminate\Contracts\Filesystem\Filesystem 契約的不同實(shí)現(xiàn)。Laravel 為此定義了簡(jiǎn)單、平滑的接口:
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\VideoController;
use App\Http\Controllers\PhotoControllers;
use Illuminate\Contracts\Filesystem\Filesystem;
$this->app->when(PhotoController::class)
->needs(Filesystem::class)
->give(function () {
return Storage::disk('local');
});
$this->app->when(VideoController::class)
->needs(Filesystem::class)
->give(function () {
return Storage::disk('s3');
});
標(biāo)簽
少數(shù)情況下,我們需要解析特定分類下的所有綁定,例如,你正在構(gòu)建一個(gè)接收多個(gè)不同 Report 接口實(shí)現(xiàn)的報(bào)告聚合器,在注冊(cè)完 Report 實(shí)現(xiàn)之后,可以通過 tag 方法給它們分配一個(gè)標(biāo)簽:
$this->app->bind('SpeedReport', function () {
//
});
$this->app->bind('MemoryReport', function () {
//
});
$this->app->tag(['SpeedReport', 'MemoryReport'], 'reports');
這些服務(wù)被打上標(biāo)簽后,可以通過 tagged 方法來輕松解析它們:
$this->app->bind('ReportAggregator', function ($app) {
return new ReportAggregator($app->tagged('reports'));
});
擴(kuò)展綁定
extend 方法允許對(duì)解析服務(wù)進(jìn)行修改。例如,當(dāng)服務(wù)被解析后,可以運(yùn)行額外代碼裝飾或配置該服務(wù)。extend 方法接收一個(gè)閉包來返回修改后的服務(wù):
$this->app->extend(Service::class, function($service) {
return new DecoratedService($service);
});
總結(jié)
到此這篇關(guān)于Laravel服務(wù)容器綁定的文章就介紹到這了,更多相關(guān)Laravel服務(wù)容器綁定內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 詳解Laravel服務(wù)容器的綁定與解析
- 詳解如何實(shí)現(xiàn)Laravel的服務(wù)容器的方法示例
- laravel ajax curd 搜索登錄判斷功能的實(shí)現(xiàn)
- Laravel中Kafka的使用詳解
- laravel使用redis隊(duì)列實(shí)例講解
- Laravel的加密解密與哈希實(shí)例講解
- Laravel中10個(gè)有用的用法小結(jié)
- 詳解Laravel服務(wù)容器的優(yōu)勢(shì)