【问题标题】:zend framework1.12 rest services for web and mobile appzend framework1.12 用于 web 和移动应用程序的 rest 服务
【发布时间】:2013-10-31 15:24:47
【问题描述】:

我是 zend 框架的新手,想创建一个提供服务的门户网站。 Web应用程序和移动应用程序都将使用这些服务。 我正在使用 Chris Danielson 的文章

http://www.chrisdanielson.com/2009/09/02/creating-a-php-rest-api-using-the-zend-framework/) 作为基础。

我的问题是我是否朝着正确的方向前进。目前我正在访问服务 http://www.zendrestexample.com/version.

1) 我要求它使用 url 作为http://www.zendrestexample.com/api/version

改为。

2)我需要使用zend restserver来编写服务吗?

3)我可以为移动应用和网络应用使用相同的服务吗?我的意思是出现任何重定向问题?

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

请帮帮我..

【问题讨论】:

    标签: zend-framework zend-rest zend-rest-route


    【解决方案1】:

    好吧,您并没有使用一堆 PHP 文件来完成这项工作...所以我认为您走在正确的轨道上 =)。文章中的实现还可以,但是很老了……写于> 4年前。我建议调查Zend_Soap_ServerZend_Json_ServerZend_Rest_Server。在我看来,肥皂解决方案对移动设备来说有点重。

    只需决定实施,少做计划!


    我编写了一个 Web 应用程序,后来不得不添加服务层,以便将移动应用程序接口添加到应用程序。不幸的是,这不是最初要求的一部分,所以不得不重做很多事情。

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

    1. 在库或控制器助手中编写所有应用程序逻辑。因此可以在主 Web 应用程序和 API 层中重用相同的代码
    2. 在默认模块中编写您的 webapp 逻辑
    3. 在名为“api”的专用模块中编码您的 api 层
    4. phpdoc 必须完美才能让 zend 自动生成 SMD

    对于使用标准 JSON-RPC 2.0 协议的 API,有适用于 Android / iPhone 的客户端使用此协议并提供自动发现(类似于 WSDL 的 SMD,但用于 json)。所有通过 GET 发送的请求都会导致 SMD 显示,所有其他请求都会导致对请求的处理。

    为您的 API 层使用 Zend_Json_Server。 这是一个功能示例:

    <?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 '';
        }
    
    }
    

    这里是示例请求(我添加了一些引导魔法来通过 id 获取会话)

    https://domain.com/api/hello-world
    {
      "session_id": "4ggskr4fhe3lagf76b5tgaiu57",
      "method": "hello2",
      "params": { 
        "name" : "Alex"
      },
      "id": 123
    }
    

    评论JSON RPC 2.0 Documentation

    我发现Advanced REST Client for Google Chrome 是开发和测试 JSON Web 服务的最佳扩展。

    为了提高安全性,您可以通过向抽象控制器添加几行代码来限制通过 HTTP Auth 的所有请求,甚至创建安全性 controller plugin

    祝你好运。

    【讨论】:

    • 感谢您的详细解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 2011-07-08
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多