douti0467 2016-11-13 17:16
浏览 684
已采纳

Laravel __construct() - 没有注入?

I am getting error:

GitHubApp::__construct() must be an instance of App\Project\Repositories\GitProviderRepository

I thought Laravel does some kind of magic when it come to __construct() so i don't have to inject it into new GitHubApp();?

  use App\Project\Repositories\GitProviderRepository;

    class GitHubApp
    {
        private $gitProviderRepository;

        public function __construct(GitProviderRepository $gitProviderRepository)
        {
            $this->gitProviderRepository = $gitProviderRepository;
        }
    }

In other class:

return new GitHubApp();
  • 写回答

2条回答 默认 最新

  • dongmo1708 2016-11-13 17:45
    关注

    When calling new GithubApp(), you relied on yourself to build the GithubApp instance, Laravel does not responsible for building that instance.

    You have to let Laravel resolve the dependencies for you. There are numbers of way to achieve this:

    Using App facade:

    App::make(GithubApp::class);
    

    Using app() helper method:

    app(GithubApp::class);
    

    Or using the resolve() helper method:

    resolve(GithubApp::class);
    

    Behind the scene, your class type and its dependencies will be resolved and instantiated by the Illuminate\Container\Container class (the parent class of Application). Specifically by the make() and build() methods.

    Hope this help! :)

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

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料