简体   繁体   中英

Symfony 2: Override symfony component

I'm searching how to override a component of 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. Is this even possible ?

Yes, it is. Here's a detailed guide on how to override any part of a 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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