Templates zend framework 2无法呈现模板解析程序无法解析为文件
我正在学习如何使用Zend Framework2。根据网上的一些教程,我写了一些代码。对我来说最重要的教程是:它涵盖了我计划编写的大部分基础知识。我遇到了一个对我来说很奇怪的问题。在显示DB中所有记录的“我的摘要”页面上,我有一个添加新记录、编辑现有记录和删除记录的链接。module.config.php涵盖了路由:Templates zend framework 2无法呈现模板解析程序无法解析为文件,templates,zend-framework,render,resolver,Templates,Zend Framework,Render,Resolver,我正在学习如何使用Zend Framework2。根据网上的一些教程,我写了一些代码。对我来说最重要的教程是:它涵盖了我计划编写的大部分基础知识。我遇到了一个对我来说很奇怪的问题。在显示DB中所有记录的“我的摘要”页面上,我有一个添加新记录、编辑现有记录和删除记录的链接。module.config.php涵盖了路由: 'router' => array( 'routes' => array( 'incid
'router' => array(
'routes' => array(
'incident' => array(
'type' => 'segment',
'options' => array(
'route' => '/incident[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Helpdesk\Controller\Incident',
'action' => 'index',
),
),
),
),
),
当我使用指向新记录的链接(h.t.t.p://helpdesk/incident/add)时,一切正常。但当我使用链接编辑我的记录(h.t.t.p://helpdesk/incident/edit/1-其中1是示例记录ID)时,我收到一个错误:
Zend\View\Renderer\PhpRenderer::render:无法呈现模板“帮助台/事件/编辑”;解析程序无法解析为文件
这是我的IncidentController.php:
<?php
namespace Helpdesk\Controller;
use Application\Controller\EntityUsingController;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject;
use Doctrine\ORM\EntityManager;
use Zend\View\Model\ViewModel;
use Helpdesk\Form\IncidentForm;
use Helpdesk\Entity\Incident;
class IncidentController extends EntityUsingController
{
/**
* Index action
*
*/
public function indexAction()
{
$em = $this->getEntityManager();
$incidents = $em->getRepository('Helpdesk\Entity\Incident')->findAll();
return new ViewModel(array(
'incidents' => $incidents
));
}
/**
* Edit action
*
*/
public function editAction()
{
$incident = new Incident();
if ($this->params('id') > 0) {
$incident = $this->getEntityManager()->getRepository('Helpdesk\Entity\Incident')->find($this->params('id'));
}
$form = new IncidentForm($this->getEntityManager());
$form->bind($incident);
$form->setHydrator(new DoctrineObject($this->getEntityManager(),'Helpdesk\Entity\Incident'));
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($incident->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$em = $this->getEntityManager();
$em->persist($incident);
$em->flush();
$this->flashMessenger()->addSuccessMessage('Incident saved');
// Redirect to list of incidents
return $this->redirect()->toRoute('incident');
}
}
return array(
'incident' => $incident,
'form' => $form,
);
}
/**
* Add action
*
*/
public function addAction()
{
return $this->editAction();
}
/**
* Delete action
*
*/
public function deleteAction()
{
$id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
if (!$id) {
return $this->redirect()->toRoute('incident');
}
$request = $this->getRequest();
if ($request->isPost()) {
$del = $request->post()->get('del', 'No');
if ($del == 'Yes') {
$id = (int)$request->post()->get('id');
$incident = $this->getEntityManager()->find('Helpdesk\Entity\Incident', $id);
if ($incident) {
$this->getEntityManager()->remove($incident);
$this->getEntityManager()->flush();
}
}
// Redirect to list of incidents
return $this->redirect()->toRoute('default', array(
'controller' => 'incident',
'action' => 'index',
));
}
return array(
'id' => $id,
'incident' => $this->getEntityManager()->find('Helpdesk\Entity\Incident', $id)->getArrayCopy()
);
}
}
最有可能的帮助台/incident/edit.phtml不存在,而添加操作正在呈现现有的帮助台/incident/add.phtml
您可以重用现有的helpdesk/incident/add.phtml或创建一个新的helpdesk/incident/add.phtml。首先,您的编辑操作将返回一个数组,而不是ViewModel。您也没有提到您有哪些视图脚本。是否有一个“./view/{…}/edit.phtml”文件需要解析。。。你是对的。没有edit.phtml。但我忘了提到,实际工作的add操作使用了edit操作:公共函数addAction(){return$this->editAction();}我认为这解释了为什么没有edit.phtml。你提到的第一件事当然是真的。我忘记创建新的ViewModel()。我已经解决了,但问题仍然存在。好吧,现在我明白你的意思了。添加了edit.phtml,问题就解决了。但是如果我正在重用(正如我之前提到的)编辑操作-如何重用add.phtml视图脚本?我可以简单地复制它的内容吗?我认为在这种情况下,它应该是可重用的……您可以简单地将操作中的模板(视图脚本)更改为您喜欢的任何内容$viewModel->setTemplate('helpdesk/incident/add')见:非常感谢您的帮助。这就是重点。:)后者对我来说非常正确。复制/粘贴工作的剩余部分。我的目标视图脚本位于模块/Consultant/view/Consultant/index/index.phtml
中。然而,一个不完整的模块Profile
(Profile在顾问之后注册,从而取代了视图脚本)正在注册(在module/Profile/config/module.config.php
)它的视图脚本与视图管理器的关系是“顾问”=>\uuuuuuuu DIR\uuuuuuuu.//查看“
”而不是“”配置文件“=>\uuuuuuu DIR\uuuuuuuu.”//查看“
”。