我使用 Zend Framework 开发我的 Web 应用程序,我想为 Android 应用程序创建一个 Web 服务,内容类型将是 JSON。
创建此网络服务的最佳方法是什么?它是一个控制器吗?这个控制器将扩展动作控制器
class ApiController extends Frontend_Controller_Action
或使用Zend_Json_Server
. 我有点困惑,zend Json Server 会比 ApiController 更好地帮助什么?
我使用 Zend Framework 开发我的 Web 应用程序,我想为 Android 应用程序创建一个 Web 服务,内容类型将是 JSON。
创建此网络服务的最佳方法是什么?它是一个控制器吗?这个控制器将扩展动作控制器
class ApiController extends Frontend_Controller_Action
或使用Zend_Json_Server
. 我有点困惑,zend Json Server 会比 ApiController 更好地帮助什么?
阅读 Zend_Rest_Controller。使用它代替 Zend_Controller_Action。这很简单。Zend_Rest_Controller 只是一个抽象控制器,其中包含您应该在控制器中实现的预定义操作列表。简短的例子,我在 Api 模块中像 Index Controller 一样使用它:
class Api_IndexController extends Zend_Rest_Controller
{
public function init()
{
$bootstrap = $this->getInvokeArg('bootstrap');
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(TRUE);
$this->_helper->AjaxContext()
->addActionContext('get','json')
->addActionContext('post','json')
->addActionContext('new','json')
->addActionContext('edit','json')
->addActionContext('put','json')
->addActionContext('delete','json')
->initContext('json');
}
public function indexAction()
{
$method = $this->getRequest()->getParam('method');
$response = new StdClass();
$response->status = 1;
if($method != null){
$response->method = $method;
switch ($method) {
case 'category':
...
break;
case 'products':
...
break;
default:
$response->error = "Method '" . $response->method . "' not exist!!!";
}
}
$content = $this->_helper->json($response);
$this->sendResponse($content);
}
private function sendResponse($content){
$this->getResponse()
->setHeader('Content-Type', 'json')
->setBody($content)
->sendResponse();
exit;
}
public function getAction()
{}
public function postAction()
{}
public function putAction()
{}
public function deleteAction()
{}
}