Laravel 6 - 在构造函数中绑定实现数组
Laravel 6 - Bind array of implementations in constructor
我正在尝试将某个接口的一组实现注入订阅者 class。
这是我的服务提供商中的代码:
$this->app->tag([TrackerServiceOne::class, TrackerServiceTwo::class], 'trackers');
$this->app->bind(EventSubscriber::class, function ($app) {
return new EventSubscriber($this->app->tagged('trackers'));
});
这是我的 EventSubscriber
class:
中的构造函数
public function __construct(array $trackers)
{
$this->trackers = $trackers;
}
这是我遇到的错误:
Unresolvable dependency resolving [Parameter #0 [ <required> array $trackers ]] in class Path\To\EventSubscriber
尝试使用服务容器中的 makeWith
解析 EventSubscriber
而不是实例化 new
实例:
$this->app->bind(EventSubscriber::class, function ($app) {
return $app->makeWith(EventSubscriber::class, [
'trackers' => $this->app->tagged('trackers')
]);
});
这应该正确地将 $trackers
参数注入到 EventSubscriber 的构造函数中。
来自Docs:
If some of your class' dependencies are not resolvable via the container, you may inject them by passing them as an associative array into the makeWith method:
$api = $this->app->makeWith('HelpSpot\API', ['id' => 1]);
更新
在尝试 Laravel 6 之后,我设法让它工作了,尽管与文档中描述的不完全一样:
app()->bind('TrackerServiceOne', function () {
return new TrackerServiceOne();
});
app()->bind('TrackerServiceTwo', function () {
return new TrackerServiceTwo();
});
app()->tag([
'TrackerServiceOne',
'TrackerServiceTwo'
], 'trackers');
app()->bind(EventSubscriber::class, function () {
$trackers = [];
foreach (app()->tagged('trackers') as $tracker) {
$trackers[] = $tracker;
}
return new EventSubscriber($trackers);
});
$subscriber = resolve(EventSubscriber::class);
dd($subscriber->getTrackers());
我正在尝试将某个接口的一组实现注入订阅者 class。
这是我的服务提供商中的代码:
$this->app->tag([TrackerServiceOne::class, TrackerServiceTwo::class], 'trackers');
$this->app->bind(EventSubscriber::class, function ($app) {
return new EventSubscriber($this->app->tagged('trackers'));
});
这是我的 EventSubscriber
class:
public function __construct(array $trackers)
{
$this->trackers = $trackers;
}
这是我遇到的错误:
Unresolvable dependency resolving [Parameter #0 [ <required> array $trackers ]] in class Path\To\EventSubscriber
尝试使用服务容器中的 makeWith
解析 EventSubscriber
而不是实例化 new
实例:
$this->app->bind(EventSubscriber::class, function ($app) {
return $app->makeWith(EventSubscriber::class, [
'trackers' => $this->app->tagged('trackers')
]);
});
这应该正确地将 $trackers
参数注入到 EventSubscriber 的构造函数中。
来自Docs:
If some of your class' dependencies are not resolvable via the container, you may inject them by passing them as an associative array into the makeWith method:
$api = $this->app->makeWith('HelpSpot\API', ['id' => 1]);
更新
在尝试 Laravel 6 之后,我设法让它工作了,尽管与文档中描述的不完全一样:
app()->bind('TrackerServiceOne', function () {
return new TrackerServiceOne();
});
app()->bind('TrackerServiceTwo', function () {
return new TrackerServiceTwo();
});
app()->tag([
'TrackerServiceOne',
'TrackerServiceTwo'
], 'trackers');
app()->bind(EventSubscriber::class, function () {
$trackers = [];
foreach (app()->tagged('trackers') as $tracker) {
$trackers[] = $tracker;
}
return new EventSubscriber($trackers);
});
$subscriber = resolve(EventSubscriber::class);
dd($subscriber->getTrackers());