我有一个由两个Symfony应用程序使用的库,这个库定义了一组我希望公开的服务(我希望能够通过容器直接检索它们。当我尝试访问一项服务时,我拥有以下内容:
在编译容器时,"Library\Service\DerivedServices\OneSpecificImplementation“服务或别名已被删除或内联。要么将其公开,要么停止直接使用容器,转而使用依赖项注入。
问题是,上述服务被宣布为公共服务。
基本上有:
Library\Service\BaseService类,它有两个用于公共依赖项的设置程序(此代码段中的理论和记录器);Library\Service\DerivedServices命名空间中),每个类定义一个新服务(有自己的构造函数直接处理DI )。下面是我的服务定义:
# Base: inject common dependencies
Library\Service\BaseService:
abstract: true
calls:
- [setDoctrine, ['@doctrine.orm.entity_manager']]
- [setLogger, ['@Library\Service\Logger']]
# These services are public to be retrieved directly by the container interface
Library\Service\DerivedServices\:
resource: '../vendor/company/library/src/Library/Service/DerivedServices'
public: true
autowire: true
autoconfigure: false
parent: Library\Service\BaseService然后,Symfony应用程序检索一个派生服务,如:
$this->get('Library\Service\DerivedServices\OneSpecificImplementation');这些并没有什么区别:
我认为这是一些琐碎的配置,但我无法精确地指出它(在试图调试框架为什么将我的服务编译为私有的两个小时之后,我认为可能有人拥有这个,并且可能会帮助我)。
发布于 2019-08-09 04:26:30
事实证明,服务声明的顺序非常重要。正如我所想的,问题是从结构上讲。
我有:
Library\Service\BaseService:
...
Library\Service\DerivedServices\:
...
Library\Service\:
resource: '../vendor/company/library/src/Library/Service'最后一条指令将所有服务重新声明为私有服务(默认情况下)。
我把这个改成:
Library\Service\:
resource: '../vendor/company/library/src/Library/Service'
Library\Service\BaseService:
...
Library\Service\DerivedServices\:
...这首先声明了所有服务的私有性,然后用新的声明重新声明了它们:使用父+公共。
https://stackoverflow.com/questions/57421463
复制相似问题