服务容器上下文绑定报错
哪位前辈帮忙答疑解惑一下
错误信息:
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);
}
推荐文章: