【发布时间】:2018-05-24 17:05:08
【问题描述】:
我正在尝试使用依赖注入来实例化我的课程。 这个类有自己的服务提供者,在 app.php 中注册
class Something
{
private $variable;
public function __construct(string $variable)
{
$this->variable = $variable;
}
}
这是服务提供商
class SomethingServiceProvider extends ServiceProvider
{
public function boot()
{
}
public function register()
{
$this->app->singleton('Something', function () {
return new Something( 'test');
});
}
}
当我尝试在控制器中使用此类实例时...
class TestController extends AppBaseController
{
public function __construct(Something $something)
{
$this->something = $something;
}
...
我收到错误:
"无法解析的依赖解析[参数#0 [字符串$variable]] 在课堂上 Container->unresolvablePrimitive(object(ReflectionParameter)) in Container.php(第 848 行)“
【问题讨论】:
-
除了其他答案,一定要在你的config/app.php中添加这个服务提供者,这样才能加载
标签: php laravel ioc-container