我正在学习如何使用Zend Framework2。根据网上提供的一些教程,我写了一些代码段。对我来说最重要的教程是这样的:https://github.com/psamatt/zf2-doctrine-example它涵盖了我计划编写的大部分基础知识。我坚持一个对我来说很陌生的问题。在我的摘要页面上,显示来自数据库的所有记录我有一个链接来添加新记录,编辑现有记录和删除记录。路由是由module.config.php覆盖:zend框架2无法呈现模板解析器无法解析到文件
'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://帮助台/事件/添加)一切正常。但是,当我使用链接编辑我的记录(HTTP://帮助台/事件/编辑/ 1 - 其中1例记录ID)我收到一个错误:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "helpdesk/incident/edit"; resolver could not resolve to a file
这是我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()
);
}
}
这两者有什么区别?为什么一个工作正常,而第二个产生错误?
感谢您的帮助
Smok。
首先,您的编辑操作正在返回一个数组,而不是ViewModel。你也没有提到你有什么视图脚本。是否有'./view/{...}/edit.phtml'文件解决? – chandlermania
恩......你说得对。没有edit.phtml。但我忘了提及,添加动作,实际工作,使用编辑动作:\t public function addAction() \t { \t \t return $ this-> editAction(); \t}我认为这解释了为什么没有edit.phtml。你提到的第一件事情是真实的。我忘了创建新的ViewModel()。我已经解决了,但问题仍然存在。 – Smok
好的 - 现在我明白你的意思了。已经添加edit.phtml,问题已经消失。但如果我正在重复使用(正如我提到的那样)编辑操作 - 如何重用add.phtml视图脚本?我应该简单地复制它的内容吗?我猜想在这种情况下它应该是可重用的...... – Smok