4

我正在使用 SonataAdminBundle 和 SonataUserBundle。

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

我该如何禁用sonata.user.admin.group?我一直在关注 Symfony2 文档中的那些食谱:

到目前为止,我的包定义中有以下代码来添加编译器通道:

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

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

这是编译器传递:

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

I thought that this should work but no. Symfony is throwing an exception telling me that sonata.user.admin.group service does not exist. But it exists, and if I do $container->getDefinition('sonata.user.admin.group') the actual definition is return.

Thanks

4

2 回答 2

9

Try marking the service as abstract and set its public property to false e.g.

#in any services.yml
services:
    sonata.user.admin.group:
      abstract: true
      public: false
    #...

Addition to completeness:

And add to the CompilerPass:

$container->getDefinition('sonata.user.admin.group')->setSynthetic(true);
于 2012-12-20T18:04:01.077 回答
1

You've removed the service definition but it's still used on the dashboard. That's why Symfony complains (dashboard tries to access it). It's not an optional service.

You could try to overwrite the dashboard template and avoid using the service? This way service wouldn't be called and you wouldn't have to remove it. If service is not used it's never created.

Alternative would be overloading the service with your implementation.

于 2012-12-20T16:48:36.673 回答