代码之家  ›  专栏  ›  技术社区  ›  Apex

Zend Framework 1.9.2+Zend_Rest_路由示例

  •  5
  • Apex  · 技术社区  · 17 年前

    随着 Zend_Rest_Route 在Zend Framework 1.9中(及其 update 在1.9.2)中,我们现在有了一个用于路由请求的标准化RESTful解决方案。截至2009年8月,没有关于其用法的示例,只有参考指南中的基本文档。

    虽然它可能比我想象的要简单得多,但我希望那些比我更有能力的人能够提供一些例子来说明 Zend_Rest_Controller 在以下情况下:

    • 某些控制器(如indexController.php)运行正常

    看来 JSON Action Helper 现在完全自动化并优化json对请求的响应,使其与Zend_Rest_路由一起使用成为理想的组合。

    2 回复  |  直到 17 年前
        1
  •  7
  •   Apex    17 年前

    看起来很简单。我已经使用Zend_Rest_控制器抽象构建了一个Restful控制器模板。只需将no_results返回值替换为包含您想要返回的数据的本机php对象。欢迎评论。

    <?php
    /**
     * Restful Controller
     *
     * @copyright Copyright (c) 2009 ? (http://www.?.com)
     */
    class RestfulController extends Zend_Rest_Controller
    {
    
        public function init()
        {
            $config = Zend_Registry::get('config');
            $this->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);
        }
    }
    
        2
  •  0
  •   user175671 user175671    17 年前

    很好的帖子,但我认为 Zend_Rest_Controller 将请求路由到与所用HTTP方法相关的正确操作。如果有一个 POST 请求 http://<app URL>/Restful 会自动 _forward postAction 例如

    下面我将继续提供另一个策略,但可能我没有抓住要点 Zend_静止控制器 ... 请评论。

    我的策略:

    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]