سؤال

I'm learning how to use Zend Framework2. According to some tutorials available on the Net I've wrote some pieces of code . The most important tutorial for me is this one: https://github.com/psamatt/zf2-doctrine-example It covers most of the basics that i've planned to write. I've stuck on one problem that looks strange to me. On my summary page, that display all the records from DB I have a links to add new record, edit existing record, and delete record. Routing is covered by module.config.php:

        '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',
                                    ),
                            ),
                    ),
            ),
    ),

When I use a link to a new record (h.t.t.p://helpdesk/incident/add) everything works correctly. But when I use a link to edit my record (h.t.t.p://helpdesk/incident/edit/1 - where 1 is example record ID) I receive an error:

Zend\View\Renderer\PhpRenderer::render: Unable to render template "helpdesk/incident/edit"; resolver could not resolve to a file

This is my 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()
    );
}
}

What is the difference between these two? Why one works fine, while the second one generates an error?

Thanks for your help

Smok.

هل كانت مفيدة؟

المحلول

Most likely helpdesk/incident/edit.phtml does not exist, while add action is rendering an existing helpdesk/incident/add.phtml.

You can reuse the existing helpdesk/incident/add.phtml or create a new one.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top