Zend Framework 1.9.2+ Zend_Rest_Route示例 [英] Zend Framework 1.9.2+ Zend_Rest_Route Examples

查看:63
本文介绍了Zend Framework 1.9.2+ Zend_Rest_Route示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 there are no examples of its usage, only the basic documentation found in the reference guide.

虽然它可能比我想象的要简单得多,但我希望那些比我更胜任的人能够提供一些示例来说明

While it is perhaps far more simple than I assume, I was hoping those more competent than I might provide some examples illustrating the use of the Zend_Rest_Controller in a scenario where:

  • 某些控制器(例如indexController.php)正常运行
  • 其他人则作为基于休息的服务(返回json)

出现 JSON Action Helper 现在可以完全自动化和优化对请求的json响应,使其与Zend_Rest_Route一起使用是理想的组合.

It appears the JSON Action Helper now fully automates and optimizes the json response to a request, making its use along with Zend_Rest_Route an ideal combination.

推荐答案

似乎很简单.我已经使用Zend_Rest_Controller Abstract整理了一个Restful Controller模板.只需将no_results返回值替换为包含要返回的数据的本机php对象.欢迎发表评论.

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.

<?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);
    }
}

这篇关于Zend Framework 1.9.2+ Zend_Rest_Route示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆