dtsps00544
2011-04-27 20:59
浏览 35
已采纳

重构Zend_Controller_Action

Ive been working on a project with ZendFramework and Doctrine2 and have noticed an issue with my action controllers not being very 'DRY'. Given the following example, which is pretty much identical throughout all of my controllers what would be the best approach to factor out the repetition. I think that a baseController would be by far the easiest, but it seems that extending Zend_Controller_Action is frowned upon. The other two options that I am leaning toward is to create an action helper or controller plugin(but controller plugins these are still a fuzzy concept to me).
So my question is, what would be the best way to do this? and why?

class Job_JobController extends Zend_Controller_Action
{

   protected $_service;

   public function init()
   {
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->addActionContext('create', 'html')
                    ->addActionContext('update', 'html')
                    ->addActionContext('view', 'html')
                    ->addActionContext('delete', 'html')
                    ->addActionContext('message', 'html')
                    ->initContext();
        $this->_service = $this->_helper->service('job');
    }

    public function indexAction()
    {
         $this->_forward('list');
    }

    public function listAction()
    {
        $grid = $this->_service->getGrid();
        $this->view->list = $grid->render();      
    }

    public function createAction()
    {
        $request = $this->getRequest();
        $formMediator = $this->_service->getFormMediator();

        if ($request->isPost() && $formMediator->isValid($request->getPost()))
        {
            $this->_service->saveEntity($formMediator->transfer());
            $this->_helper->flashMessenger->addMessage(array('statusCode' => 'Create::Success'));
            $this->_forward('message');
        }

        $this->view->entity = $formMediator->getForm();       
    }

    public function updateAction()
    {
        $request = $this->getRequest();
        $formMediator = $this->_service->getFormMediator();
        $entity = $this->_service->findEntity($request->getParam('id'));

        if ($request->isPost() && $formMediator->isValid($request->getPost()))
        {
            $this->_service->saveEntity($formMediator->transfer($entity));
            $this->_helper->flashMessenger->addMessage(array('statusCode' => 'Update::Success'));
            $this->_forward('message');
        } 
        else
        {
            $formMediator->populate($entity);
        }

        $this->view->entity = $formMediator->getForm();
    }


    public function viewAction()
    {
        $request = $this->getRequest();
        $this->view->entity = $this->_service->getEntity($request->getParam('id'));
    }

    public function deleteAction()
    {
        $request = $this->getRequest();
        $id = $request->getParam('id');

        if ($this->getRequest()->isPost())
        {
            $this->_service->delete($request->getParam('id'));
            $this->_helper->flashMessenger->addMessage(array('statusCode' => 'Delete::Success'));
            $this->_forward('message'); 
         }

         $this->view->deleteId = $id;
    }

    public function messageAction()
    {
         $messages = array_merge((array)$this->_helper->flashMessenger->getMessages(), 
                     (array)$this->_helper->flashMessenger->getCurrentMessages());
         $this->view->messages = $messages;
    }

}

3条回答 默认 最新

相关推荐 更多相似问题