简体   繁体   English

Symfony 2:覆盖symfony组件

[英]Symfony 2: Override symfony component

I'm searching how to override a component of symfony. 我正在搜索如何覆盖symfony的组件。 In my specific case I would like to override the MessageCatalogue class of the Translation component to be able to send event when no translations have been found. 在我的特定情况下,我想重写Translation组件的MessageCatalogue类,以便在未找到翻译时发送事件。 Is this even possible ? 这有可能吗?

Yes, it is. 是的。 Here's a detailed guide on how to override any part of a bundle . 这是有关如何覆盖bundle的任何部分的详细指南。 The first example shows what you are looking for. 第一个示例显示您要查找的内容。

// src/Acme/DemoBundle/DependencyInjection/Compiler/OverrideServiceCompilerPass.php
namespace Acme\DemoBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class OverrideServiceCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $definition = $container->getDefinition('original-service-id');
        $definition->setClass('Acme\DemoBundle\YourService');
    }
}

However, xabbuh's comment on your question seems to be an easier solution. 但是,xabbuh对您的问题的评论似乎是一个更简单的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM