2017-07-09 40 views
1

我想在注入類(資源庫)我app/Console/Kernel如何在Laravel的app/console/Kernel中注入一個類?

public function __construct(LocaleRepository $localeRepository) 
{ 
    $this->_localeRepository = $localeRepository; 
} 

不幸的是,我收到以下錯誤,這並不工作: PHP Fatal error: Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [App\Repositories\Interfaces\LocaleRepository] is not instantiable while building [App\Console\Kernel]. in /home/cv/cus/vendor/laravel/framework/src/Illuminate/Container/Container.php:895

我可以在注入庫控制器沒有任何問題。該庫還登記在服務提供商:

public function register() 
{ 
    $this->app->bind('App\Repositories\Interfaces\LocaleRepository', 'App\Repositories\Implementations\EloquentLocaleRepository'); 
} 

是否有可能在app/console/Kernel類注射類?

回答

0

我通過調用一個單獨的方法在register方法的第一行中注入接口來解決此問題。

在這個單獨的方法,我有以下代碼: $this->_localeRepository = $this->app->make('App\Repositories\Interfaces\LocaleRepository');

0

在加載應用服務提供者之前實例化類app/console/Kernel

所以,我不認爲有可能將類注入構造函數。

但是,您可以使用方法注入。只需將資源庫注入您需要的方法即可。

2

使用app('App\Repositories\Interfaces\LocaleRepository');以計劃方法獲取您的界面對象,它會工作。

+0

爲我工作,謝謝 – FosAvance