问题描述
所以在这个问题上经过数小时后,我必须承认我被卡住了,我不明白我应该做什么。 我正在尝试使特定控制器在 symfony 项目中呈现自定义模板。 让我解释: 我目前正在从事的项目使用奏鸣曲作为管理部分,我被要求在奏鸣曲模板之一的顶部添加文件的内容。 所以我所做的是进入管理这个页面并从 Admin 奏鸣曲类扩展的自定义控制器,添加一个包含我的文件内容的新变量,然后我首先让奏鸣曲在奏鸣曲配置文件中呈现我的自定义模板,如下所示:
templates:
layout: SonataAdminBundle::standard_layout.html.twig
ajax: SonataAdminBundle::ajax_layout.html.twig
dashboard: SonataAdminBundle:Core:dashboard.html.twig
list: *my custom template path*
问题是,我希望它仅在调用此控制器时才呈现,因此其他页面不会在页面顶部显示额外的文本。所以我试着跟着这个https://symfony.com/doc/current/bundles/SonataAdminBundle/reference/templates.html 问题是我几乎不明白这里发生了什么,奏鸣曲文档(或 symfony 一个,因为它最终使用奏鸣曲文档)没有解释任何东西,而且似乎easyAdmin 教程中没有任何在线应用程序。我也试图遵循这一点:https://sonata-project.org/bundles/admin/master/doc/reference/advanced_configuration.html,但它仍然根本不起作用。
这是我在每个文件中所做的。
自定义控制器的名称叫做cardReccurentDebitAdmin,所以就像文档建议我制作了一个cardReccurentCRUD.PHP文件如下:
<?
namespace Comiti\AppBundle\Admin;
use Sonata\AdminBundle\Controller\CRUDController;
class CardReccurentCRUD extends CRUDController{
public function listAction(){
$request = $this->getRequest();
$this->admin->checkAccess('list');
$preResponse = $this->preList($request);
if (null !== $preResponse) {
return $preResponse;
}
if ($listMode = $request->get('_list_mode')) {
$this->admin->setListMode($listMode);
}
$datagrid = $this->admin->getDatagrid();
$formView = $datagrid->getForm()->createView();
// set the theme for the current Admin Form
$this->setFormTheme($formView,$this->admin->getFilterTheme());
// NEXT_MAJOR: Remove this line and use commented line below it instead
$template = 'pathToFile';
// $template = $this->templateRegistry->getTemplate('list');
return $this->renderWithExtraParams($template,[
'action' => 'list','form' => $formView,'datagrid' => $datagrid,'csrf_token' => $this->getCsrftoken('sonata.batch'),'export_formats' => $this->has('sonata.admin.admin_exporter') ?
$this->get('sonata.admin.admin_exporter')->getAvailableFormats($this->admin) :
$this->admin->getExportFormats(),],null);
}
}
这里我尝试通过创建 services.ylm 文件来实现:
services:
app.bundle1.app_bundle.admin:
class: path\CardRecurrentDebitAdmin
arguments:
- ~
- ~
- path\Admin\CardRecurrentCRUD
calls:
- [setTemplate,['custom','/var/www/html/src/Comiti/AppBundle/Resources/views/Admin/test.html.twig']]
tags:
- { name: sonata.admin,manager_type: orm,group: Demo,label: Card Recurrent Debit }
最后文件 CardRecurrentDebitAdmin 和 CardRecurrentCRUD 的位置如下:
App/src/folder/bundle1/Admin/CardRecurrentCRUD
我真的希望有人能向我解释我做错了什么或以其他方式实现我的目标。我想要的只是让控制器 CardRecurrentDebitAdmin 使用自定义模板,并且只使用这个控制器。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)