Symfony 如何在别名服务上添加标记?
我定义了以下服务:Symfony 如何在别名服务上添加标记?,symfony,Symfony,我定义了以下服务: my_project.widget_listing_content_resolver: class: MyProject\Widget\ListingBundle\Resolver\WidgetListingContentResolver arguments: - "@router" tags: - { name: my_project.widget_content_resolver, alias: Listing }
my_project.widget_listing_content_resolver:
class: MyProject\Widget\ListingBundle\Resolver\WidgetListingContentResolver
arguments:
- "@router"
tags:
- { name: my_project.widget_content_resolver, alias: Listing }
我想声明此服务的别名,使用不同的标记:
my_project.widget_domain_operations_content_resolver:
alias: my_project.widget_listing_content_resolver
tags:
- { name: my_project.widget_content_resolver, alias: DomainOperations }
但在我的ContentResolverCain中,别名为“DomainOperations”的服务不存在。有办法解决这个问题吗
编辑:
我尝试了以下配置:
my_project.widget_listing_content_resolver:
class: MyProject\Widget\ListingBundle\Resolver\WidgetListingContentResolver
arguments:
- "@router"
tags:
- { name: my_project.widget_content_resolver, alias: Listing }
- { name: my_project.widget_content_resolver, alias: DomainOperations }
结果,“my_project.widget_listing_content_resolver”服务只标记为“listing”。我现在的问题是:“如何使用多个标记别名标记服务”我找到了解决该问题的方法。服务别名已标记为espected,但我的CompilerPass未读取additionnal标记:
$taggedServices = $container->findTaggedServiceIds(
'my_project.widget_content_resolver'
);
foreach ($taggedServices as $id => $attributes) {
$definition->addMethodCall(
'addResolver',
array($attributes[0]['alias'], new Reference($id))
);
}
这是出错的编译器程序:
$taggedServices = $container->findTaggedServiceIds(
'my_project.widget_content_resolver'
);
foreach ($taggedServices as $id => $attributes) {
$definition->addMethodCall(
'addResolver',
array($attributes[0]['alias'], new Reference($id))
);
}
如您所见,它只使用找到的第一个别名($attributes[0])
我不得不把它改成:
$taggedServices = $container->findTaggedServiceIds(
'my_project.widget_content_resolver'
);
foreach ($taggedServices as $id => $attributes) {
foreach ($attributes as $attribute) {
$definition->addMethodCall(
'addResolver',
array($attribute['alias'], new Reference($id))
);
}
}
Alias允许您为服务创建另一个名称,而不是更改其定义。您可能想创建一个新服务。我想定义一个新名称,并为服务“my_project.widget\u domain\u operations\u content\u resolver”添加一个新标记,但我将使用相同的类和相同的参数。Alias似乎是在第二个服务中重新声明具有相同参数的同一类的一个很好的替代方法,只是为了添加一个标记。我挖掘了一点,似乎标记别名确实有效,但标记名称相同,因此标记被覆盖,这就是为什么标记别名“DomainOperation”不显示我发现了错误的位置,在definal中,服务别名可以创建另一个名称,并可以在别名服务上添加一些标记。谢谢你的帮助