Laravel IoC遇到麻烦时绑定

时间:2018-08-28 08:53:30

标签: laravel dependency-injection inversion-of-control laravel-jobs

我无法绑定到容器。 这是我在ServiceProvider中的注册方法

    $this->app->bind('dash_manager', function () {
        return new CoinsManager(new CoinManagerFactory(), 'dash');
    });

    $this->app->bind('litecoin_manager', function () {
        return new CoinsManager(new CoinManagerFactory(), 'ltc');
    });

    $this->app->when(CreateDashWalletJob::class)
        ->needs(CoinsManager::class)
        ->give('dash_manager');

    $this->app->when(CreateLitecoinWalletJob::class)
        ->needs(CoinsManager::class)
        ->give('dash_manager');

我使用带有CoinManager注入的handle方法创建了Job

public function handle(CoinsManager $dashManager, ...)
{
     //logic
}

和莱特币的相同工作。 但是...

Illuminate\Contracts\Container\BindingResolutionException: Unresolvable 
dependency resolving [Parameter #1 [ <required> $currency ]] in class 
App\Libraries\CoinsManager\CoinsManager

我试图做这样的事情

$this->app->when(CreateLitecoinWalletJob::class)
        ->needs('$currency')
        ->give('ltc');

还有这个

$this->app->when(CreateLitecoinWalletJob::class)
        ->needs(CoinManager::class)
        ->needs('$currency')
        ->give('ltc');

但是它接收到相同的异常

0 个答案:

没有答案