无法在 SonataAdminBundle 模板中使用 render(controller)

Can't use render(controller) in SonataAdminBundle template

我创建自定义控制器(扩展 Sonata\AdminBundle\Controller\CRUDController)和操作。

渲染这个动作时{{render(controller('MainBundle:SonataAdmin/Order:searchCertificate'))}}

我收到 Symfony 错误:

An exception has been thrown during the rendering of a template ("There is no _sonata_admin defined for the controller MainBundle\Controller\SonataAdmin\OrderController and the current route").

我在官方找到了答案documentation:

If you want to render a custom controller action in a template by using the render function in twig you need to add _sonata_admin as an attribute. For example; {{ render(controller('AppBundle:XxxxCRUD:comment', {'_sonata_admin': 'sonata.admin.xxxx' })) }}. This has to be done because the moment the rendering should happen the routing, which usually sets the value of this parameter, is not involved at all, and then you will get an error "There is no _sonata_admin defined for the controller AppBundleControllerXxxxCRUDController and the current route ' '."

我已经通过在提交请求中设置 _sonata_admin 解决了这个问题:

使用普通控制器:

$request->request->set('_sonata_admin','admin.template');