1

我想将我最近设置的 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

FooServiceBuzService实现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”。您是否创建了实现此接口的类?

为什么我会收到此错误以及如何使此结构正常工作?

4

1 回答 1

2

此错误消息看起来像 Symfony 错误消息,而不是 PHP-DI 错误消息。

我猜 Symfony 会扫描你所有的类以进行自动装配,包括你的控制器(即使它不是必需的)。

这是我第一次听说这个,我猜你需要完全禁用 Symfony 的自动装配,或者某些特定的文件夹?如果您可以向 PHP-DI 的文档发送拉取请求,那就太棒了:)

于 2018-03-19T07:23:56.280 回答