服务容器上下文绑定报错

哪位前辈帮忙答疑解惑一下
错误信息:
Target [App\Contracts\MyTest\IsTest] is not instantiable.

TestServiceProvider.php

public function register()
    {
        //
//        $this->app->singleton('TestT',Test::class);
        /*$this->app->singleton(
            \App\Contracts\MyTest\IsTest::class,
                \App\Units\Test\Contracts::class
        );*/
//        $this->app->bind(
//            \App\Contracts\MyTest\IsTest::class,
//            \App\Units\Test\Contracts::class
//        );
//        $this->app->bind(
//            \App\Contracts\MyTest\IsTest::class,
//            \App\Units\Test\Test::class
//        );
    // 上面的代码正常
        // 上下文绑定
        // IsTest 是一个接口,Contracts和Test是实现
        $this->app->when(HomeController::class)
            ->needs(IsTest::class)
            ->give(\App\Units\Test\Contracts::class);

        $this->app->when(TestController::class)
            ->needs(IsTest::class)
            ->give(\App\Units\Test\Test::class);
    }
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案
  $this->app->when(HomeController::class)
            ->needs(IsTest::class)
            ->give(\App\Units\Test\Contracts::class);
当调用HomeController里面当方法的时候 需要__construct注入接口类 IsTest,给他\App\Units\Test\Contracts::class,。

\App\Units\Test\Contracts::class implements IsTest::class


5年前 评论
rsonly (楼主) 5年前
musickr 4年前
w564315651 3年前
讨论数量: 5
  $this->app->when(HomeController::class)
            ->needs(IsTest::class)
            ->give(\App\Units\Test\Contracts::class);
当调用HomeController里面当方法的时候 需要__construct注入接口类 IsTest,给他\App\Units\Test\Contracts::class,。

\App\Units\Test\Contracts::class implements IsTest::class


5年前 评论
rsonly (楼主) 5年前
musickr 4年前
w564315651 3年前

我也是,试了一上午了,不知道怎么用

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!