Zend Framework 1.9.2+ Zend_Rest_Route Examples

后端 未结 2 1601
轻奢々
轻奢々 2021-02-06 11:43

With the introduction of Zend_Rest_Route in Zend Framework 1.9 (and its update in 1.9.2) we now have a standardized RESTful solution for routing requests. As of August 2009 ther

2条回答
  •  情深已故
    2021-02-06 12:32

    great post, but I would have thought the Zend_Rest_Controller would route the request to the right action with respect to the HTTP method used. It'd be neat if a POST request to http:///Restful would automatically _forward to postAction for example.

    I'll go ahead and provide another strategy below, but maybe I'm missing the point behind Zend_Rest_Controller ... please comment.

    My strategy:

    class RestfulController extends Zend_Rest_Controller
    {
    
        public function init()
        {
         $this->_helper->viewRenderer->setNoRender();
      $this->_helper->layout->disableLayout();
        }
    
        public function indexAction()
        {
            if($this->getRequest()->getMethod() === 'POST')
                 {return $this->_forward('post');}
    
            if($this->getRequest()->getMethod() === 'GET')
                 {return $this->_forward('get');}
    
            if($this->getRequest()->getMethod() === 'PUT')
                 {return $this->_forward('put');}
    
            if($this->getRequest()->getMethod() === 'DELETE')
                 {return $this->_forward('delete');}
    
            $this->_helper->json($listMyCustomObjects);
    
        }
        // 1.9.2 fix
        public function listAction() { return $this->_forward('index'); }
    
    [the rest of the code with action functions]
    

提交回复
热议问题