duanli9591 2017-07-09 16:36
浏览 109
已采纳

如何在Laravel的app / console / Kernel中注入一个类?

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?

  • 写回答

3条回答 默认 最新

  • duanjieyi6582 2017-07-18 16:05
    关注

    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');

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制