Clean Ajax controller implementation in Zend Framework

后端 未结 4 707
独厮守ぢ
独厮守ぢ 2021-02-10 08:48

I need a controller in my Zend Framework project, which should handle only ajax requests.

My approach at the moment is to extend the Zend_Controller_Action:



        
4条回答
  •  遇见更好的自我
    2021-02-10 09:42

    I have done much the same as you, I have a controller specifically for handling ajax requests, however my solution is much simpler, I have use the init() function rather than preDispatch.

    My init() function looks like this

    class Ajax_Controller extends Zend_Controller_Action
    {
        public function init()
        {
            $this->_helper->layout()->disableLayout();
        }
    
        //the rest of the controller...
    }
    

    That's it! I have made no other changes from a standard controller.

    I disable view rendering in the action if required as I found, in my case, I needed a view to render tables etc.. However, if you only ever return json data then your method of disabling view rendering in preDispatch() is perfectly valid.

    If I need to return json I have a similar function to yours and I disable view rendering.

    I have used this method on 4 seperate, but similar, projects now without any issues. Having said that my ajax calls are usually pretty simple, but I don't see any issues with this method.

    If you are looking for a simple, clean ajax implementation, then this may be an option for you.

提交回复
热议问题