Zend Framework 1.9.2+ Zend_Rest_Route Examples

后端 未结 2 1599
轻奢々
轻奢々 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:27

    Appears it was rather simple. I've put together a Restful Controller template using the Zend_Rest_Controller Abstract. Simply replace the no_results return values with a native php object containing the data you want returned. Comments welcome.

    db = Zend_Db::factory($config->resources->db);
            $this->no_results = array('status' => 'NO_RESULTS');
        }
    
        /**
         * List
         *
         * The index action handles index/list requests; it responds with a
         * list of the requested resources.
         * 
         * @return json
         */
        public function indexAction()
        {
            // do some processing...
            // Send the JSON response:
            $this->_helper->json($this->no_results);
        }
        // 1.9.2 fix
        public function listAction() { return $this->_forward('index'); }
    
        /**
         * View
         *
         * The get action handles GET requests and receives an 'id' parameter; it 
         * responds with the server resource state of the resource identified
         * by the 'id' value.
         * 
         * @param integer $id
         * @return json
         */
        public function getAction()
        {
            $id = $this->_getParam('id', 0);
    
            // do some processing...
            // Send the JSON response:
            $this->_helper->json($this->no_results);
        }
    
        /**
         * Create
         *
         * The post action handles POST requests; it accepts and digests a
         * POSTed resource representation and persists the resource state.
         * 
         * @param integer $id
         * @return json
         */
        public function postAction()
        {
            $id = $this->_getParam('id', 0);
            $my = $this->_getAllParams();
    
            // do some processing...
            // Send the JSON response:
            $this->_helper->json($this->no_results);
        }
    
        /**
         * Update
         *
         * The put action handles PUT requests and receives an 'id' parameter; it 
         * updates the server resource state of the resource identified by 
         * the 'id' value.
         * 
         * @param integer $id
         * @return json
         */
        public function putAction()
        {
            $id = $this->_getParam('id', 0);
            $my = $this->_getAllParams();
    
            // do some processing...
            // Send the JSON response:
            $this->_helper->json($this->no_results);
        }
    
        /**
         * Delete
         *
         * The delete action handles DELETE requests and receives an 'id' 
         * parameter; it updates the server resource state of the resource
         * identified by the 'id' value.
         * 
         * @param integer $id
         * @return json
         */
        public function deleteAction()
        {
            $id = $this->_getParam('id', 0);
    
            // do some processing...
            // Send the JSON response:
            $this->_helper->json($this->no_results);
        }
    }
    

提交回复
热议问题