Nik*_*hev 1 php service bundle symfony symfony4
我有一个包含接口的包,Optimax\HealthCheckBundle\Service\HealthInterface
我需要为实现此接口的所有服务设置标签。我使用以下指令执行此操作:
_instanceof:
Optimax\HealthCheckBundle\Service\HealthInterface:
tags: ['health.service']
Run Code Online (Sandbox Code Playgroud)
当我将此指令放入config/services.yaml
. 但是,如果我将此代码放入我的包的配置中(需要通过作曲家),vendor/optimax/health-check/src/Resources/config/services.yaml
则它不起作用。services.yaml
当我需要这个包到一个新项目时,我不想每次都复制粘贴这个指令。
如何将此指令移动到services.yaml
我的 Bundle 目录中或至少移动到config/packages
项目文件夹中的另一个文件中?
为其他人扩展这个问题。
_instanceof
功能与_defaults
定义相同。因为该_instanceof
定义仅适用于使用它的文件。
这可以防止第三方包定义影响您的整个应用程序,例如:
_defaults:
public: true
autowire: false
_instanceof:
Psr\Log\LoggerAwareInterface:
- method: setLogger
arguments:
- '@custom_logger'
tags:
- { name: monologer.log, channel: 'custom_channel' }
Run Code Online (Sandbox Code Playgroud)
因此,如果您尝试标记的服务_instanceof
未在同一services.yml
文件中声明,则不会添加标记。
要标记在整个应用程序中实现接口的服务,您需要使用@Jeroen 提供的方法
您是否尝试在 Bundle 扩展中使用此接口自动标记所有服务,如下所示:
$container->registerForAutoconfiguration(CustomInterface::class)
->addTag('app.custom_tag')
;
Run Code Online (Sandbox Code Playgroud)
摘自 Symfony 文档: https://symfony.com/doc/current/service_container/tags.html