Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony 3.4。Can';不要覆盖服务_Php_Symfony_Symfony 3.4 - Fatal编程技术网

Php Symfony 3.4。Can';不要覆盖服务

Php Symfony 3.4。Can';不要覆盖服务,php,symfony,symfony-3.4,Php,Symfony,Symfony 3.4,我正试图覆盖“供应商”部分的某些服务。按照这个指南,我编写了这个代码 namespace AppBundle\DependencyInjection\Compiler; use AppBundle\Service\Subscriber; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuild

我正试图覆盖“供应商”部分的某些服务。按照这个指南,我编写了这个代码

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');
   }
}

您必须在src/AppBundle/AppBundle.php中添加此代码:

函数内构建()

所有类别:

class AppBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        parent::build($container);

        $container->addCompilerPass(new OverrideServiceCompilerPass());
    }
}


否则您的编译器类将无法加载。

对于覆盖服务的特定情况,您只需在bundle(或app folder)services.yml中定义一个新服务,如下所示

       lexik_jwt_authentication.security.guard.jwt_token_authenticator:
       class: SeguridadBundle\DependencyInjection\MyJWTTokenAuthenticator
       arguments: ["@lexik_jwt_authentication.jwt_manager", "@event_dispatcher", "@lexik_jwt_authentication.extractor.chain_extractor"]
当然,有一些规则:

  • 服务的名称必须与供应商中的名称完全相同。因此,我上面写的一个覆盖了LexikJWTAuthentication供应商中另一个名为
    lexik\u jwt\u authentication.security.guard.jwt\u token\u authenticator
    的服务
  • 指定的类将是具有您自己实现的类
因此,当重写服务时,您需要创建一个新的定义,该定义保持与原始定义相同的名称,但使用不同的类进行实例化。 请注意,对于兼容性问题,最好实现与原始服务相同的接口,即:遵循与原始服务相同的约定,以保证兼容性


对不起,我的英语不好,我希望它能帮助你

你能验证你是否有正确的服务id来覆盖吗?试着在setclass之后转储$definition,看看使用服务“key”语法(
my.very.own.service.
)会有什么结果。最近,如果遵循官方文件,但这并没有很大帮助。
       lexik_jwt_authentication.security.guard.jwt_token_authenticator:
       class: SeguridadBundle\DependencyInjection\MyJWTTokenAuthenticator
       arguments: ["@lexik_jwt_authentication.jwt_manager", "@event_dispatcher", "@lexik_jwt_authentication.extractor.chain_extractor"]