我正在使用zend框架构建一个应用程序。
问题是,如何在非rest应用程序中使用Zend_Rest_Controller的相同控制器逻辑。
例如,假设twitter是用Zend Framework编写的。他们可能会使用Zend_Rest_controller和Route作为他们的API。然而,他们会为他们的网站使用什么(显然使用相同的API逻辑)?他们会写一个全新的应用程序来触发REST请求吗?这不是超负荷了吗?
[编辑]
如果web应用程序通过一些http_client类调用API来获取数据,则会向服务器发出另一个请求(这会导致性能下降并减慢响应速度)。我不想发出另一个请求,并希望使用API中相同的业务逻辑。
谢谢,
举止文雅
新答案:
我想出了一个似乎很有效的模式。它解决了你所有的担忧:以下是我想到的一个缩小版:
首先我们需要自己的控制器。这个控制器将有一个服务,它将代理对该服务的任何操作请求,如果它们没有定义:
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以便:
- 我们可以直接访问请求对象
- 我们可以很容易地从任何控制器调用服务
这将充当应用程序和实际数据存储之间的桥梁。
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)
我希望这对你有帮助。这对我来说很有效。
免责声明:我从来没有这样做过,我不知道这是否可行。
由于Zend_Rest_Controller扩展Zend_Controller_Action与现在真正的rest特定的逻辑,除了一些抽象的方法,你可以让你的网站控制器扩展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
}
}
如果你的rest控制器的动作是返回值,如db对象或数组基于发生了什么,然后你可以使用返回的数据做特定于网站的逻辑。
需要考虑的一件事是,如果你正在使用json动作帮助器为你的rest 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;
}
黑客快乐!