Zend框架Restful webservice逻辑使用

发布于 2024-11-03 18:16:38 字数 409 浏览 1 评论 0原文

我正在使用 zend 框架构建一个应用程序。

问题是,如何在非 REST 应用程序中使用 Zend_Rest_Controller 的相同控制器逻辑。

例如,我们假设 twitter 是用 Zend Framework 编写的。他们可能会使用 Zend_Rest_controller 和 Route 作为他们的 API。然而,他们的网站会使用什么(显然使用相同的 API 逻辑)?他们会编写一个全新的应用程序来简单地触发 REST 请求吗?这不是超载吗。

[编辑]
如果 Web 应用程序通过某个 http_client 类调用 API 来获取数据,则会向服务器发出另一个请求(这会导致性能下降并减慢响应速度)。我不想发出另一个请求,并且想使用 API 中的相同业务逻辑。

谢谢,
维努

I am building an application using zend framework.

The question is, how to use the same controller logic of the Zend_Rest_Controller within a non-REST app.

For instance, let's assume twitter was written with Zend Framework. They would probably use the Zend_Rest_controller and Route for their API. However, what would they use for their website (which obviously use the same API logic)? Would they write an entire new application that simply fire REST request? Isn't that overload.

[EDIT]
If web app calls API through some http_client class to get the data, that makes another request to server (it causes performance degrade and slow down the response). I don't want to make another request and want to use the same business logic which is in API.

Thanks,
Venu

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

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

发布评论

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

评论(2

各自安好 2024-11-10 18:16:38

新答案:

我想出了一种似乎效果很好的模式。它解决了您所有的担忧:
这是我想出的缩小版本:

首先我们需要自己的控制器。该控制器将有一个服务,如果未定义,它将代理任何操作请求到该服务:

abstract class App_Rest_Controller extends Zend_Controller_Action
{
    /** 
     * @var App_Rest_Service_Abstract
     */
    protected $_service;

    public function __call($methodName, $args)
    {
        if ('Action' == substr($methodName, -6)) {
            $action = substr($methodName, 0, strlen($methodName) - 6);
            return $this->_service()->$action();
        }

        return parent::__call($methodName, $args);
    }
}

现在是该服务的时间了。我们扩展了 Action Helper Abstract,以便:

  1. 我们可以直接访问请求对象
  2. 我们可以轻松地从任何控制器调用服务

这将充当应用程序和数据实际存储之间的桥梁。

abstract class App_Rest_Service_Abstract extends Zend_Controller_Action_Helper_Abstract
{
    /*
     * @var App_Rest_Storage_Interface
     */
    protected $_storage;
    public function __call($methodName, $args)
    {
        if (!method_exists($this->getStorage(), $methodName)) {
            throw new App_Rest_Service_Exception(sprintf('The storage does not have the method "%s"', $methodName));
        }

        switch ($methodName) {
            case 'get':
            case 'put':
            case 'delete':
                //if id param isnot set, throw an exception
                if (FALSE === ($id = $this->getRequest()->getParam('id', FALSE))) {
                    throw new  App_Rest_Service_Exception(sprintf('Method "%s" expects an id param, none provided', $methodName));
                }
                $iterator = $this->getStorage()->$methodName($id, $this->getRequest()->getParams());
                break;
            case 'index':
            case 'post':
            default:
                //if index, post or not a tradition RESTful request, the function must expect the first and only argument to be an array
                $iterator =  $this->getStorage()->$methodName($this->getRequest()->getParams());
                break;
        }


        return $this->_getResult($iterator);
    }

    protected function _getResult($iterator)
{ /*
       * write your own, in my case i make a paginator and then
       *  either return it or send data via the json helper
       * 
      /*
}

现在来说说界面。这将完成存储、修改和返回数据的实际工作。将它用作接口的好处在于,无论模型层使用什么,都可以轻松实现它。我创建了一个抽象存储,它只有一个 Zend_Form(用于验证)和一个用于实际数据的 Zend_Db_Table。但您也可以在任何对象上实现它。

interface App_Rest_Storage_Interface extends Zend_Validate_Interface
{
    public function index(array $params = NULL);

    public function get($id, array $params = NULL);

    public function post(array $params);

    public function put($id, array $params);

    public function delete($id, array $params);

}

现在可以在您站点内的任何位置进行操作。假设您有“客户”服务。在任何控制器内部,它都像

$customer = $this->_helper->helper->customers->get(1);

其他地方一样简单(例如视图助手):

Zend_Controller_Action_HelperBroker::getStaticHelper('customers')->get(1)

我希望这会有所帮助。它对我来说效果很好。

New Answer:

I have come up with a pattern that seems to work well. It solves all of your concerns:
Here is a scaled down version of what I came up with:

First we need our own controller. This conroller will have a service where by it proxies any action request to the service, if they are not defined:

abstract class App_Rest_Controller extends Zend_Controller_Action
{
    /** 
     * @var App_Rest_Service_Abstract
     */
    protected $_service;

    public function __call($methodName, $args)
    {
        if ('Action' == substr($methodName, -6)) {
            $action = substr($methodName, 0, strlen($methodName) - 6);
            return $this->_service()->$action();
        }

        return parent::__call($methodName, $args);
    }
}

Now its time for the service. We extend Action Helper Abstract so that:

  1. we have direct access to the request object
  2. we can easily call up the service from any controller

This will act a go between for the the application and the actual storage of the data.

abstract class App_Rest_Service_Abstract extends Zend_Controller_Action_Helper_Abstract
{
    /*
     * @var App_Rest_Storage_Interface
     */
    protected $_storage;
    public function __call($methodName, $args)
    {
        if (!method_exists($this->getStorage(), $methodName)) {
            throw new App_Rest_Service_Exception(sprintf('The storage does not have the method "%s"', $methodName));
        }

        switch ($methodName) {
            case 'get':
            case 'put':
            case 'delete':
                //if id param isnot set, throw an exception
                if (FALSE === ($id = $this->getRequest()->getParam('id', FALSE))) {
                    throw new  App_Rest_Service_Exception(sprintf('Method "%s" expects an id param, none provided', $methodName));
                }
                $iterator = $this->getStorage()->$methodName($id, $this->getRequest()->getParams());
                break;
            case 'index':
            case 'post':
            default:
                //if index, post or not a tradition RESTful request, the function must expect the first and only argument to be an array
                $iterator =  $this->getStorage()->$methodName($this->getRequest()->getParams());
                break;
        }


        return $this->_getResult($iterator);
    }

    protected function _getResult($iterator)
{ /*
       * write your own, in my case i make a paginator and then
       *  either return it or send data via the json helper
       * 
      /*
}

Now for the interface. This will do the actual work of storing, modifying and returning data. The beauty of using it as an interface is that you can easily implement it no matter what you use for the model layer. I created an abstract Storage that simply has a Zend_Form (for validation) and a Zend_Db_Table for the actual data. but you could also implement it on any object.

interface App_Rest_Storage_Interface extends Zend_Validate_Interface
{
    public function index(array $params = NULL);

    public function get($id, array $params = NULL);

    public function post(array $params);

    public function put($id, array $params);

    public function delete($id, array $params);

}

Now operation anywhere within your site. Suppose you have an "Customers" service. Inside any controller its as simple as

$customer = $this->_helper->helper->customers->get(1);

anywhere else (say a view helper for example):

Zend_Controller_Action_HelperBroker::getStaticHelper('customers')->get(1)

I hope this helps. It is working well for me.

旧时光的容颜 2024-11-10 18:16:38

免责声明:我从未这样做过,我不知道它的可行性如何。

由于 Zend_Rest_Controller 使用现在真正的 Rest 特定逻辑(除了一些抽象方法)扩展了 Zend_Controller_Action,因此您可以让您的网站控制器扩展 Rest 控制器。示例:

class Web_IndexController extends Rest_IndexController
{
    public function IndexAction() {
        //do whatever your rest contrller would do
        $result = parent::indexAction();
        //add website specific specific logic here
    }
}

如果您的其余控制器的操作返回值,例如基于发生的情况的数据库对象或数组,则您可以使用返回的数据来执行特定于网站的逻辑。

需要考虑的一件事是,如果您使用 json 操作助手来返回其余 api 的值,您应该构建一个控制器属性来抑制发送和退出。示例:

class Rest_IndexController extends Zend_Rest_Controller
{

    protected $_sendJson = TRUE;
    public function IndexAction() {
        $this->_helper->json($data, $this->_sendJson);
    }
}

class Web_IndexController extends Rest_IndexController
{
    protected $_sendJson = FALSE;
}

黑客快乐!

Disclaimer: I've never done this, I don't know how feasible it is.

Since Zend_Rest_Controller extends Zend_Controller_Action with now real rest specific logic except for a few abstract methods, you could just have your website controllers extend the Rest controller. Example:

class Web_IndexController extends Rest_IndexController
{
    public function IndexAction() {
        //do whatever your rest contrller would do
        $result = parent::indexAction();
        //add website specific specific logic here
    }
}

If your rest controller's actions are returning values, such as db objects or arrays based upon what happened, you could then use the returned data to do web site-specific logic.

One thing to think about, if you are using the json action helper to return values for your rest api, you should build in a controller property to suppress the sending and exiting. Example:

class Rest_IndexController extends Zend_Rest_Controller
{

    protected $_sendJson = TRUE;
    public function IndexAction() {
        $this->_helper->json($data, $this->_sendJson);
    }
}

class Web_IndexController extends Rest_IndexController
{
    protected $_sendJson = FALSE;
}

Happy Hacking!

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