开发者

Refactoring 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 followi开发者_C百科ng 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;
    }

}


There's nothing wrong with extending the Zend_Controller_Action. For our software we extended it and use the preDispatch() function to verify that the user has authenticated and display a login screen if they have not. Now all our controllers extend that one, and we don't have to do anything for all our pages to require a login.


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.


I think an abstract controller should be fine, although I sometimes find it a little inelegant because testing the abstract class' behaviour is tricky.

One thing you could do having seen this code is Replace Temp with Query. You're making temp variable assignments for little purpose other than saving a trivial amount of typing (and sometimes typing even more than you need to). Sometimes it's worth typing a few extra characters, to reduce the number of temp/local variables in your methods which aids refactoring.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜