Zend framework zend framework1.12 web和移动应用程序的rest服务

Zend framework zend framework1.12 web和移动应用程序的rest服务,zend-framework,zend-rest,zend-rest-route,Zend Framework,Zend Rest,Zend Rest Route,我是zend framework的新手,希望创建一个提供服务的web门户。 Web应用程序和移动应用程序都将使用这些服务。 我在用克里斯·丹尼尔森的文章 )作为基础 我的问题是我的方向是否正确。目前我正在以 1) 我要求它使用url作为 相反 2) 我是否需要使用zend restserver来编写服务 3) 我可以为移动应用程序和web应用程序使用相同的服务吗?我的意思是会出现重定向问题吗 4) 我需要使用rest客户端来使用这些服务吗 请帮帮我吧。好吧,你没有使用一堆PHP文件来完成这项工

我是zend framework的新手,希望创建一个提供服务的web门户。 Web应用程序和移动应用程序都将使用这些服务。 我在用克里斯·丹尼尔森的文章

)作为基础

我的问题是我的方向是否正确。目前我正在以

1) 我要求它使用url作为

相反

2) 我是否需要使用zend restserver来编写服务

3) 我可以为移动应用程序和web应用程序使用相同的服务吗?我的意思是会出现重定向问题吗

4) 我需要使用rest客户端来使用这些服务吗


请帮帮我吧。

好吧,你没有使用一堆PHP文件来完成这项工作。。。所以我认为你的思路是对的。本文中的实现还可以,但是非常旧。。。是4年前写的。我建议调查一下,或者。在我看来,Soap解决方案对于移动设备来说有点沉重

只需决定实施方案,很少做计划


我编写了一个web应用程序,后来不得不添加服务层,以便将移动应用程序界面添加到应用程序中。不幸的是,这不是最初需求的一部分,因此必须重新做很多事情

我的建议如下(如果您的webapp和api在同一个项目中):

  • 在库或控制器帮助程序中编写所有应用程序逻辑。因此,相同的代码可以在主web应用程序和API层中重用
  • 在默认模块中编码您的webapp逻辑
  • 在名为“api”的专用模块中编写api层
  • phpdoc必须是完美的,以便zend自动生成SMD
  • 对于使用标准JSON-RPC 2.0协议的API,Android/iPhone的客户端都使用该协议并提供自动发现(类似于SMD的WSDL,但用于JSON)。通过SMD中的GET result发送的所有请求都将显示,所有其他请求都将处理该请求

    利用您的API层。 以下是一个功能示例:

    <?php
    
    // API Controller Example
    class ApiController extends Zend_Controller_Action
    {
    
        public function init()
        {
            parent::init();
            $this->getHelper('ViewRenderer')->setNoRender();
        }
    
        public function helloWorldAction()
        {
            $this->_handleRequest('App_Api_HelloWorld');
        }
    
        protected function _handleRequest($handlerClassName)
        {
            //
            $this->getHelper('ViewRenderer')->setNoRender();
    
            //
            $server = new Zend_Json_Server();
            $server->setClass($handlerClassName);
            if ($_SERVER['REQUEST_METHOD'] == 'GET') {
                $cfg = Zend_Registry::get('config');
                $req = $this->getRequest();
                $reqUrl = $cfg->paths->basehref . $req->getControllerName() . '/' . $req->getActionName();
    
                $server->setTarget($reqUrl)
                        ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
                $smd = $server->getServiceMap();
    
                header('Content-Type: application/json');
                echo $smd;
            } else {
    
                //  handle request
                $server->handle();
            }
        }
    
    }
    
    
    
    // HANDLER Class Example
    
    class App_Api_HelloWorld extends App_Api_ApiHandlerAbstract
    {
    
        /**
         * says "hello world"
         * 
         * @return string
         */
        public function hello()
        {
            return 'hello world';
        }
    
        /**
         * says "hello $name"
         * 
         * @param string $name
         * @return string
         */
        public function hello2($name)
        {
            return "hello $name";
        }
    
        /**
         * 
         * @return string
         * @throws Exception
         */
        public function hello3()
        {
    
            throw new Zend_Json_Server_Exception('not allowed');
            return '';
        }
    
    }
    
    复习

    我发现Google Chrome是开发和测试JSON web服务的最佳扩展

    为了获得额外的安全性,您可以通过向抽象控制器添加几行代码甚至创建安全性来限制所有请求


    祝你好运。

    谢谢你的详细解释。
    https://domain.com/api/hello-world
    {
      "session_id": "4ggskr4fhe3lagf76b5tgaiu57",
      "method": "hello2",
      "params": { 
        "name" : "Alex"
      },
      "id": 123
    }