2013-07-09 113 views
2

我正在学习如何使用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。

+0

首先,您的编辑操作正在返回一个数组,而不是ViewModel。你也没有提到你有什么视图脚本。是否有'./view/{...}/edit.phtml'文件解决? – chandlermania

+0

恩......你说得对。没有edit.phtml。但我忘了提及,添加动作,实际工作,使用编辑动作:\t public function addAction() \t { \t \t return $ this-> editAction(); \t}我认为这解释了为什么没有edit.phtml。你提到的第一件事情是真实的。我忘了创建新的ViewModel()。我已经解决了,但问题仍然存在。 – Smok

+0

好的 - 现在我明白你的意思了。已经添加edit.phtml,问题已经消失。但如果我正在重复使用(正如我提到的那样)编辑操作 - 如何重用add.phtml视图脚本?我应该简单地复制它的内容吗?我猜想在这种情况下它应该是可重用的...... – Smok

回答

1

最有可能的helpdesk/incident/edit.phtml不存在,而添加操作是渲染现有的helpdesk/incident/add.phtml。

您可以重复使用现有的helpdesk/incident/add.phtml或创建一个新的helpdesk/incident/add.phtml。

+0

后者对我自己来说非常真实。复制/粘贴工作的遗留物。我的目标视图脚本位于'module/Consultant/view/consultant/index/index.phtml'内。然而,一个不完整的模块'Profile'(Profile在Consultant之后注册,因此取代了视图脚本)正在注册(在'module/Profile/config/module.config.php'中),它的view_manager视图脚本是'consultant'=> __DIR__ 。 '/../ view''而不是''profile'=> __DIR__。 “/../ view''。 –