I using Zend Framework for Developing my web application , I want to create a Web services for an Android application , the content type will be JSON.
我使用Zend Framework开发我的Web应用程序,我想为Android应用程序创建Web服务,内容类型将是JSON。
what is the best way to create this webservice ? is it a Controller and this Controller will extend the Action controller
创建此Web服务的最佳方法是什么?它是一个控制器,这个控制器将扩展Action控制器
class ApiController extends Frontend_Controller_Action
or to use Zend_Json_Server
. I am little confused , what the zend Json Server will help better than ApiController ?
或者使用Zend_Json_Server。我有点困惑,Zend Json Server比ApiController有什么帮助?
1 个解决方案
#1
5
Read about Zend_Rest_Controller. Use it instead of Zend_Controller_Action. It very simple. Zend_Rest_Controller is just an abstract controller with a list of predefined Actions that you should implement in your Controllers. Short example, I used it like Index Controller in Api module:
阅读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()
{}
}
#1
5
Read about Zend_Rest_Controller. Use it instead of Zend_Controller_Action. It very simple. Zend_Rest_Controller is just an abstract controller with a list of predefined Actions that you should implement in your Controllers. Short example, I used it like Index Controller in Api module:
阅读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()
{}
}