Zend 框架 1.9.2+ Zend_Rest_Route 示例

发布于 2024-08-03 08:24:14 字数 929 浏览 5 评论 0原文

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

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

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

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

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:

  • Some controllers (such as indexController.php) operate normally
  • Others operate as rest-based services (returning json)

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖凝香 2024-08-10 08:24:14

看起来相当简单。我使用 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);
    }
}

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);
    }
}
酒解孤独 2024-08-10 08:24:14

很棒的帖子,但我本以为 Zend_Rest_Controller 会根据所使用的 HTTP 方法将请求路由到正确的操作。如果对 http:///RestfulPOST 请求能够自动 _forwardpostAction,那就太好了例如。

我将继续在下面提供另一个策略,但也许我错过了 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]

great post, but I would have thought the Zend_Rest_Controller would route the request to the right action with respect to the HTTP method used. It'd be neat if a POST request to http://<app URL>/Restful would automatically _forward to postAction for example.

I'll go ahead and provide another strategy below, but maybe I'm missing the point behind Zend_Rest_Controller ... please comment.

My strategy:

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文