json - 在 Zend 框架中为 Android/Iphone 应用程序创建 Json Web 服务时使用什么

标签 json zend-framework

我使用 Zend Framework 开发我的 Web 应用程序,我想为 Android 应用程序创建一个 Web 服务,内容类型将是 JSON。

创建此网络服务的最佳方式是什么? 它是一个 Controller 并且这个 Controller 将扩展 Action controller

class ApiController extends Frontend_Controller_Action

或者使用 Zend_Json_Server 。 我有点困惑,zend Json Server 比 ApiController 有什么帮助?

最佳答案

阅读 Zend_Rest_Controller。使用它代替 Zend_Controller_Action。这很简单。 Zend_Rest_Controller 只是一个抽象 Controller ,其中包含您应该在 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()
     {}
}

关于json - 在 Zend 框架中为 Android/Iphone 应用程序创建 Json Web 服务时使用什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15326301/

相关文章:

php - 在 Zend DB Table Select 中使用 SQL_CALC_FOUND_ROWS 获取总行数

java - Android 可打包列表<String[]>

php - 在 Zend_HTTP_Client 中跳过 SSL 检查

json - ARM 模板 - 如何删除参数字符串中的空格?

Javascript根据ID过滤数据

zend-framework - Zend 框架与 Google Chart

php - 自动从 PHP 类生成类图?

php - Zend_Form_Element_Captcha 异常问题...?

c# - 使用 Json.Net 反序列化时出现错误 "Cannot deserialize the current JSON array"

java - 读取服务器发送的数据时出现空字符串