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条回答 默认 最新

  • dongxiee0744 2011-04-27 21:14
    关注

    Not a real answer, but I think the both ways (extending Zend_Controller_Action, or Action-Helper) are the solutions you are looking for. I dont see any reason, why an abstract controller is somewhat ... ugly(?), too. Its the way OOP works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能