使用Symfony 4.1.3声明服务时如何从自动装配中排除域?

时间:2018-09-12 08:12:32

标签: symfony service domain-driven-design autowired

我正在使用Symfony 4开发轻型DDD应用程序。在我的services.yaml文件中,我将自动装配配置为:

services:
# default configuration for services in *this* file
_defaults:
    autowire: true      # Automatically injects dependencies in your services.
    autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
    public: false       # Allows optimizing the container by removing unused services; this also means
                        # fetching services directly from the container via $container->get() won't work.
                        # The best practice is to be explicit about your dependencies anyway.

# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
    resource: '../src/*'
    exclude: '../src/{DataFixtures,Migrations,Tests,Domain/IceCream/IceCream.php,Domain/Cake/Cake.php,Domain/Candy/Candy.php}'

我排除了所有实体,因为它们不是服务。您可能已经注意到,我列出了所有相应的文件,因为当我键入时:

exclude: '../src/{DataFixtures,Migrations,Tests,Domain}'

引发运行时异常:Cannot autowire service : "App\Application\Query\Cake\CakesQueryHandler": argument "$cakeRepository" of method "__construct()" references interface "App\Domain\Cake\CakeRepositoryInterface" but no such service exists. You should maybe alias this interface to the existing "App\Infrastructure\Doctrine\Repository\Cake\CakeRepository" service. 第一个服务是查询处理程序,未自动接线。

如何排除整个域而不必在其中键入所有文件?

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

正如您直接对我说的那样,您遇到的运行时错误是:

zero

在查询处理程序中,要注入类型为 (1/1) RuntimeException Cannot autowire service "App\Application\Query\Cake\CakesQueryHandler": argument "$cakeRepository" of method "__construct()" references interface "App\Domain\Cake\CakeRepositoryInterface" but no such service exists. You should maybe alias this interface to the existing "App\Infrastructure\Doctrine\Repository\Cake\CakeRepository" service. 的服务。

事实上,您已经为类别存储库声明了名称为App\Domain\Cake\CakeRepositoryInterface的服务。

要解决此问题,您需要从界面中将别名添加到App\Infrastructure\Doctrine\Repository\Cake\CakeRepository文件中的存储库中:

services.yaml