我正在尝试覆盖“vendor”部分的某些服务。遵循这个指南https://symfony.com/doc/3.4/bundles/override.html,我编写了以下代码:
namespace AppBundle\DependencyInjection\Compiler;
use AppBundle\Service\Subscriber;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class OverrideServiceCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$definition = $container->getDefinition('MyOldService');
$definition->setClass(Subscriber::class); //my new service class
}
}
在我创建了一个名为“AppBundle\Service\Subscriber”的订阅者类并尝试覆盖动作后:
<?php
namespace AppBundle\Service;
class Subscriber
{
public function the_same_name_of_function_from_vendor()
{
dump('I am a new function!');die;
return new Response('ok');
}
}
但是什么也没有发生,Symfony继续从“vendor”部分调用函数。
我该如何正确地覆盖这个函数?