Reputation: 1364
I'm trying to inject a class (a repository) in my app/Console/Kernel
:
public function __construct(LocaleRepository $localeRepository)
{
$this->_localeRepository = $localeRepository;
}
Unfortunately this doesn't work as I receive the following error:
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
I can inject the repository in controllers without any problem. The repository is also registered in the service provider:
public function register()
{
$this->app->bind('App\Repositories\Interfaces\LocaleRepository', 'App\Repositories\Implementations\EloquentLocaleRepository');
}
Is it possible to inject a class in the app/console/Kernel
class?
Upvotes: 2
Views: 1556
Reputation: 1364
I solved this by calling a separate method to inject the interface in the first line of the register
method.
In this separate method I have the following code:
$this->_localeRepository = $this->app->make('App\Repositories\Interfaces\LocaleRepository');
Upvotes: 0
Reputation: 438
Use app('App\Repositories\Interfaces\LocaleRepository');
to get your interface object in schedule method and it'll work.
Upvotes: 3
Reputation: 1929
class app/console/Kernel
is instantiated before the app service provider is loaded.
So, I don't think it's possible to inject the class into the constructor.
However, you could use method injection. Just inject the repository into the method in which you need it.
Upvotes: 1