gpt4 book ai didi

json - Zend 框架 1.9.2+ Zend_Rest_Route 示例

转载 作者:行者123 更新时间:2023-12-04 14:53:06 25 4
gpt4 key购买 nike

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

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

  • 部分 Controller (如indexController.php)运行正常
  • 其他人作为基于休息的服务(返回 json)

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

    最佳答案

    看起来还是比较简单的。我已经使用 Zend_Rest_Controller Abstract 组合了一个 Restful Controller 模板。只需将 no_results 返回值替换为包含您要返回的数据的 native 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);
    }
    }

    关于json - Zend 框架 1.9.2+ Zend_Rest_Route 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1343724/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com