PHP / Zend Framework 2 / CRUD - How to structure controller for several actions, several views?

915 views Asked by At

Preface

I am working on a web application for which I have been adapting from an existing ASP.NET web forms app I had written previously. Most of my logic for that app was in the back end code behind.

Following creation of the application and my desire to change the app to another technology using MVC, I decided to use PHP/Zend Framework 2 so that I will develop my understanding of MVC and PHP so that I can make the code more structured.

What I have done so far...

I have the details form that can already create a new record, (i.e. Create part of the CRUD).

So far my view folder looks like this

 view
   |
   --action-item
       |
       --details
       |    |
       |    --index.phtml    <--single view template to handle CRUD
       |
       --summary
            |
            --index.phtml    <--separate view not related to details

Desired Route Template

  localhost/actionitem/create             <-- C (Create New Record)
  localhost/actionitem/view/1             <-- R (Read first element)
  localhost/actionitem/update/1           <-- U (Update first element)
  localhost/actionitem/delete/1           <-- D (Delete first element)

Controller Action Methods

/* @var $actionItemTable ActionItemTable */
    public function summaryAction()
    {
        return new ViewModel(array(
            'actionitems' => $this->getActionItemTable()->fetchAll(),
        ));       
    }

    public function detailsAction()
    {
        $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        $form = new \Application\Form\ActionItemForm($dbAdapter);
        return ['form' => $form];
    }

    public function createAction()
    {
        $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        $form = new \Application\Form\ActionItemForm($dbAdapter);

        $request = $this->getRequest();
        if($request->isPost())
        {
            $request = $this->getRequest();
            $actionitem = new ActionItem();

            if($request->isPost())
            {               
                $form->setData($request->getPost());
                if ($form->isValid())
                {
                    $actionitem->exchangeArray($form->getData());
                    $this->getActionItemTable()->saveActionItem($actionitem);
                }
            }
        }

        return ['form' => $form];
    }

Question

What is the appropriate way to structure the actions to handle CRUD operations, and is it a good idea to divide the controller into separate controllers?

Any help or suggestions would be appreciated...

1

There are 1 answers

0
Wilt On BEST ANSWER

Maybe you should think about going for a RESTful approach.

ZF2 has an AbstractRestfulController class for this. Using this controller you can easily map create, read update and delete actions to their respective POST, GET, PUT/PATCH and DELETE http methods.