我正在使用SonataAdminBundle和SonataUserBundle。
SonataUserBundle注册了一个服务sonata.user.admin.group
,该服务被SonataAdminBundle自动检测到,以设置管理仪表板中的链接来组CRUD操作。
如何禁用sonata.user.admin.group
?我一直在遵循Symfony2文档中的配方:
-
如何覆盖Bundle的任何部分- Services and Configuration
-
编译容器-创建编译器通道
-
使用容器参数和定义
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new CompilerPass());
}
这里是编译器传递:
<?php
namespace NSServiceCompilerPass;
use SymfonyComponentDependencyInjectionCompilerCompilerPassInterface;
use SymfonyComponentDependencyInjectionContainerBuilder;
class CompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container->removeDefinition('sonata.user.admin.group');
}
}
我认为这应该工作,但没有。Symfony抛出一个异常,告诉我sonata.user.admin.group
服务不存在。但是它是存在的,如果我做$container->getDefinition('sonata.user.admin.group')
,实际的定义是返回。
谢谢
尝试将服务标记为抽象,并将其public
属性设置为false,例如
#in any services.yml
services:
sonata.user.admin.group:
abstract: true
public: false
#...
添加完整性:
并添加到CompilerPass:
$container->getDefinition('sonata.user.admin.group')->setSynthetic(true);
您已经删除了服务定义,但它仍然在仪表板上使用。这就是Symfony抱怨的原因(仪表板试图访问它)。这不是可选服务
你可以尝试覆盖仪表板模板,避免使用该服务?这样服务就不会被调用,你也不必删除它。如果服务没有被使用,它将永远不会被创建。