在标准上,所有内容都放在 App 命名空间下 . 因此,如果我将 TestController 具有 public function xxx(TestInterface $test); 这样的结构一切正常 .

-src
 -Core
  -TestInterface (App\Core)
 -Api
  -Controller
   -TestController (App\Api\Controller)
 -Domain
  -Service
   -TestService implements TestInterface (App\Domain\Service)

现在的问题是我的项目没有 App 前缀 . 我的结构看起来像这样:

-src
 -Core
  -TestInterface (Core)
 -Api
  -Controller
   -TestController (Api\Controller)
 -Domain
  -Service
   -TestService implements TestInterface (Domain\Service)

composer.json

"autoload": {
    "psr-4": {
        "": "src/"
    }
},

services.yaml

Core\:
    resource: '../src/Core/*'
    exclude: '../src/Core/{Entity,Migrations,Tests,Kernel.php}'

Domain\:
    resource: '../src/Domain/*'
    exclude: '../src/Domain/{Entity,Migrations,Tests,Kernel.php}'

Api\Controller\:
    resource: '../src/Api/Controller'
    tags: ['controller.service_arguments']

最有趣的部分是几乎所有东西都运行良好,包括依赖注入,只要我使用直接类命名空间 . 仅通过界面查找不起作用 .

This works: public function xxx(TestService $test)

This doesn't work: public function xxx(TestInterface $test)

Here is the error:

无法解析“Api \ Controller \ TestController :: xxx()”的参数$ test:无法自动装配服务“.service_locator.EcpiaYx”:它引用“Core \ TestInterface”接口,但不存在此类服务 . 您应该将此接口别名为现有的“Domain \ TestService”服务 .