块类型";sonata.admin.block.admin_list";不存在


The block type "sonata.admin.block.admin_list" does not exist

我刚试着用Sonata Admin捆绑包安装Symfony CMF,但在启用它时遇到了一些问题。显然我不是第一个,但其他答案对我没有多大帮助。

这是我的完全例外:

在第35行的SonataAdminBundle:Core:dashboard.html.twig中呈现模板期间引发异常("块类型"sonata.admin.block.admin_list"不存在"(。

这是我的AppKernel.php:

$bundles = array(
    // Symfony Standard Edition Bundles
    new Symfony'Bundle'FrameworkBundle'FrameworkBundle(),
    new Symfony'Bundle'SecurityBundle'SecurityBundle(),
    new Symfony'Bundle'TwigBundle'TwigBundle(),
    new Symfony'Bundle'MonologBundle'MonologBundle(),
    new Symfony'Bundle'SwiftmailerBundle'SwiftmailerBundle(),
    new Symfony'Bundle'AsseticBundle'AsseticBundle(),
    new Doctrine'Bundle'DoctrineBundle'DoctrineBundle(),
    new Sensio'Bundle'FrameworkExtraBundle'SensioFrameworkExtraBundle(),
    // Symfony CMF Standard Edition Bundles
    new Doctrine'Bundle'PHPCRBundle'DoctrinePHPCRBundle(),
    new Doctrine'Bundle'DoctrineCacheBundle'DoctrineCacheBundle(),
    new Symfony'Cmf'Bundle'CoreBundle'CmfCoreBundle(),
    new Symfony'Cmf'Bundle'ContentBundle'CmfContentBundle(),
    new Symfony'Cmf'Bundle'RoutingBundle'CmfRoutingBundle(),
    new Symfony'Cmf'Bundle'SimpleCmsBundle'CmfSimpleCmsBundle(),
    new Symfony'Cmf'Bundle'BlockBundle'CmfBlockBundle(),
    new Sonata'BlockBundle'SonataBlockBundle(),
    new Sonata'CoreBundle'SonataCoreBundle(),
    new Symfony'Cmf'Bundle'MenuBundle'CmfMenuBundle(),
    new Knp'Bundle'MenuBundle'KnpMenuBundle(),
    new Symfony'Cmf'Bundle'CreateBundle'CmfCreateBundle(),
    new FOS'RestBundle'FOSRestBundle(),
    new JMS'SerializerBundle'JMSSerializerBundle(),
    new Sonata'jQueryBundle'SonatajQueryBundle(),
    new Sonata'DoctrinePHPCRAdminBundle'SonataDoctrinePHPCRAdminBundle(),
    new Sonata'AdminBundle'SonataAdminBundle(),
    // Remove this Bundle when using the SE as the basis for a new project
    new Acme'DemoBundle'AcmeDemoBundle(),
);

我的相关配置yml部分:

# Sonata
sonata_block:
    default_contexts: [cms]
    blocks:
        sonata.admin.block.admin_list:
          contexts: [admin]
        sonata.user.block.menu:
        sonata.user.block.account:
        sonata.block.service.text:

最后我的路线:

admin:
    resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
    prefix: /admin
_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /admin

如果有任何帮助,我们将不胜感激,我相信我已经按照说明进行了操作,我唯一的改变是将我的IP添加到app_dev.php。我还尝试过清除我的缓存,但没有任何结果。。。。

该错误表明您的sonata_block配置未被读取,或者可能被覆盖。

你能检查一下你的配置文件(config*.yml(中是否有重复的定义吗?

我对此进行了一段时间的思考,通过在Sonata'BlockBundle'Block'BlockLoaderChain::load()中添加块加载程序的调试转储,我可以看到我的块加载程序在if ($loader->support($block)) {...上默认返回true。这胜过了Sonata服务加载程序,后者应该是支持(和加载(该块的服务加载程序。装载机:

array:2 [▼
  0 => BlockLoader {#617 ▼
    #entityManager: DoctrineORMEntityManager_000000005e8b2927000000004be687142a8746960d6f2b18cb3522b67eef8a8d {#208 …2}
    #types: array:3 [▼
      0 => "cmf.block.action"
      1 => "cmf.block.container"
      2 => "cmf.block.simple"
    ]
  }
  1 => ServiceLoader {#618 ▼
    #types: array:1 [▼
      0 => "sonata.admin.block.admin_list"
    ]
  }
]

原因是我的加载程序正在扩展ServiceLoader,而->support($block)只是在检查块元数组是否包含"type"。

因此,我将自己的support()方法添加到我的块加载器中,它现在只支持我定义的类型。这是整个加载程序:

namespace ExampleBundle'Cms'Loader;
use Doctrine'ORM'EntityManager;
use Doctrine'ORM'EntityRepository;
use Sonata'BlockBundle'Block'Loader'ServiceLoader;
use Sonata'BlockBundle'Model'BlockInterface;
/**
 * ORM block loader.
 */
class BlockLoader extends ServiceLoader
{
    /**
     * @var string
     */
    const TYPE_ACTION = 'cmf.block.action';
    /**
     * @var string
     */
    const TYPE_CONTAINER = 'cmf.block.container';
    /**
     * @var string
     */
    const TYPE_SIMPLE = 'cmf.block.simple';
    /**
     * @var 'Doctrine'ORM'EntityManager
     */
    protected $entityManager;
    /**
     * {@inheritdoc}
     */
    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
        parent::__construct(array(
            self::TYPE_ACTION,
            self::TYPE_CONTAINER,
            self::TYPE_SIMPLE,
        ));
    }
    /**
     * {@inheritdoc}
     */
    public function load($configuration): BlockInterface
    {
        $blockDefault = parent::load($configuration);
        $block = $this->getRepository($configuration['type'])->findOneByName($configuration['name']);
        return $block instanceof BlockInterface ? $block : $blockDefault;
    }
    /**
     * Get the repository for a given block type.
     *
     * @param string $type The block type.
     *
     * @return 'Doctrine'ORM'Repository
     */
    protected function getRepository(string $type): EntityRepository
    {
        foreach ($this->entityManager->getMetadataFactory()->getAllMetadata() as $metadata) {
            if ($metadata->discriminatorValue === $type) {
                $baseName = str_replace($metadata->namespace . '''', '', $metadata->name);
                return $this->entityManager->getRepository(sprintf('CmsBlock:%s', $baseName));
            }
        }
        throw new 'RuntimeException(sprintf('Could not find repository for block type "%s%.', $type));
    }
    /**
     * {@inheritdoc}
     */
    public function support($configuration)
    {
        if (parent::support($configuration) && $this->exists($configuration['type'])) {
            return true;
        }
        return false;
    }
}