![](/img/trans.png)
[英]Symfony 5 bundle services.yaml - no extension able to load the configuration
[英]Symfony 4 _instanceof in Bundle's services.yaml
我有一个具有Optimax\\HealthCheckBundle\\Service\\HealthInterface
接口的捆绑软件,我需要为实现此接口的所有服务设置标签。 我使用以下指令进行操作:
_instanceof:
Optimax\HealthCheckBundle\Service\HealthInterface:
tags: ['health.service']
当我将此指令放入config/services.yaml
时,它可以正常工作。 但是,如果我将此代码放入包的配置中(通过作曲家需要),则vendor/optimax/health-check/src/Resources/config/services.yaml
将无法正常工作。 当我需要将此捆绑包添加到新项目时,我不想每次都将此指令复制粘贴到services.yaml
。
如何将该指令移至Bundle目录中的services.yaml
或至少移至项目的config/packages
文件夹中的另一个文件中?
您是否尝试过在Bundle扩展程序中使用此接口自动标记所有服务,如下所示:
$container->registerForAutoconfiguration(CustomInterface::class)
->addTag('app.custom_tag')
;
取自Symfony文档: https : //symfony.com/doc/current/service_container/tags.html
为其他人扩展这个问题。
_instanceof
功能与_defaults
定义相同。 _instanceof
定义仅适用于使用它的文件。
这样可以防止第三方捆绑软件定义影响您的整个应用程序,例如:
_defaults:
public: true
autowire: false
_instanceof:
Psr\Log\LoggerAwareInterface:
- method: setLogger
arguments:
- '@custom_logger'
tags:
- { name: monologer.log, channel: 'custom_channel' }
因此,如果您尝试使用_instanceof
标记的服务未在同一services.yml
文件中声明,则不会添加标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.