禁用SonataUserBundle sonata.user.admin.group服务

Wai*_*... 4 php dependency-injection symfony fosuserbundle sonata-admin

我正在使用SonataAdminBundle和SonataUserBundle.

SonataUserBundle注册SonataAdminBundle sonata.user.admin.group自动检测的服务,以设置管理仪表板中的链接以对CRUD操作进行分组.

我怎么禁用sonata.user.admin.group?我一直在Symfony2文档中遵循这些配方:

到目前为止,我在bundle定义中有以下代码来添加编译器传递:

public function build(ContainerBuilder $container)
{
  parent::build($container);

  $container->addCompilerPass(new CompilerPass());
}
Run Code Online (Sandbox Code Playgroud)

这里是编译器传递:

<?php

namespace NS\Service\CompilerPass;

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

class CompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
       $container->removeDefinition('sonata.user.admin.group');
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为这应该有效但不行.Symfony正在抛出异常,告诉我sonata.user.admin.group服务不存在.但它存在,如果我这样做$container->getDefinition('sonata.user.admin.group'),实际的定义是返回.

谢谢

Mun*_*Das 9

尝试将服务标记为抽象,并将其public属性设置为false,例如

#in any services.yml
services:
    sonata.user.admin.group:
      abstract: true
      public: false
    #...
Run Code Online (Sandbox Code Playgroud)

除了完整性:

并添加到CompilerPass:

$container->getDefinition('sonata.user.admin.group')->setSynthetic(true);
Run Code Online (Sandbox Code Playgroud)