6

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

虽然它可能比我想象的要简单得多,但我希望那些比我更有能力的人提供一些示例来说明Zend_Rest_Controller在以下场景中的使用:

  • 部分控制器(如indexController.php)运行正常
  • 其他的作为基于rest的服务运行(返回json)

看起来JSON Action Helper现在完全自动化并优化了对请求的 json 响应,使其与 Zend_Rest_Route 一起使用是一个理想的组合。

4

2 回答 2

7

看来还是比较简单的。我已经使用 Zend_Rest_Controller Abstract 组合了一个 Restful Controller 模板。只需将 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);
    }
}
于 2009-08-27T22:42:49.877 回答
0

很棒的帖子,但我原以为Zend_Rest_Controller会根据所使用的 HTTP 方法将请求路由到正确的操作。例如,如果POST请求http://<app URL>/Restful会自动_forward发送,那就太好postAction了。

我将继续在下面提供另一种策略,但也许我错过了背后的要点Zend_Rest_Controller......请发表评论。

我的策略:

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]
于 2009-09-18T17:39:45.140 回答