我想将我最近设置的 Symfony 4 项目与 PHP-DI 6 和 PHP-DI Symfony Bridge 3 一起使用。
我的项目结构如下所示:
|-config
|---dependencies
|-----common.php
...
|-src
...
|-Interop
|---Api
|---Website
|-----Controller
|-------IndexController.php
...
|---Services
|-----Dummy
|-------FooService.php
|-------FooServiceInterface.php
...
|-Kernel.php
类FooService
和BuzService
实现FooServiceInterface
.
/config/dependencies/common.php
return [
IndexController::class => DI\create(IndexController::class),
FooServiceInterface::class => DI\create(FooService::class),
];
IndexController
获取FooServiceInterface
注入的实例。
public function __construct(FooServiceInterface $fooService)
{
$this->fooService = $fooService;
}
Kernel
扩展DI\Bridge\Symfony\Kernel
并实现了它的buildPHPDIContainer(...)
:
protected function buildPHPDIContainer(PhpDiContainerBuilder $builder)
{
$builder->addDefinitions(__DIR__ . '/../config/dependencies/common.php');
return $builder->build();
}
Everythings 似乎是根据PHP-DI Symfony Bridge 文档设置的。
如果只有一个FooServiceInterface
. 但是当我再添加一个时,例如:
class BuzService implements FooServiceInterface
我收到一个错误:
运行时异常
无法自动连接服务“App\Interop\Website\Controller\IndexController”:方法“__construct()”的参数“$fooService”引用接口“App\Services\Dummy\FooServiceInterface”,但不存在此类服务。您可能应该将此接口别名为以下现有服务之一:“App\Services\Dummy\BuzService”、“App\Services\Dummy\FooService”。您是否创建了实现此接口的类?
为什么我会收到此错误以及如何使此结构正常工作?