Sonata Admin 搜索功能:尚未设置 Request 对象

Sonata Admin search feature : The Request object has not been set

我想在 symfony 迁移到版本 3.4.2 后激活 sonata admin 搜索功能。 我在奏鸣曲块服务中发现了一个问题:

An exception has been thrown during the rendering of a template ("The Request object has not been set").

这是奏鸣曲块配置:

sonata_block.yml

sonata_block:
    default_contexts: [cms]
    blocks:
        sonata.admin.block.admin_list:
            contexts:   [admin]
        sonata.admin.block.search_result:
            contexts: [admin]
        sonata.block.service.text:
        sonata.block.service.rss:

有什么解决办法吗?

您在使用 SonataTranslationsBundle 吗?这是 fixed recently。也许你应该更新?如果不是,请打开一个问题并提供堆栈跟踪。

解决方法是重写get请求函数,因为她抛出异常:

  public function getRequest()
    {
        if (!$this->request) {
            throw new \RuntimeException('The Request object has not been set');
        }

        return $this->request;
    }

所以我创建了一个 class 来扩展抽象管理并使我的管理文件夹中的每个 class 都从它扩展:

class MYAdmin extends AbstractAdmin
{
    /**
     * {@inheritdoc}
     */
    public function getRequest()
    {
        if (!$this->request) {
            return $this->request = $this->getConfigurationPool()->getContainer()->get('request_stack')->getCurrentRequest();
        }
        return $this->request;
    }
}