dpjs2005 2012-06-02 13:54
浏览 54
已采纳

是否需要使用RESTful Api作为自定义Web应用程序后端?

I am currently working on a web application which will use a lot of ajax for data retrieval. Certain parts of the data should only be accessible if a user is authenticated. I would down the road like to have an easy to use API for other applications but currently the only use of the API is driving the web application.

Is it worth it for me to implement a REST API vs a more customized traditional one?

I have looked into using rest and so far I have found the following

  • User authentication would be more difficult due to the server not storing state
  • The api would be unable to server multiple data items in the same request leading to a larger overhead
  • Rest api would be magnitudes more extendable
  • 写回答

1条回答 默认 最新

  • dsjzmrz78089 2012-06-02 14:18
    关注

    Well, you've given the 3 best reasons I can think of that would go into answering your question. It comes down to the answers that only you can give to those questions.

    My approach has been to have a "mini" API in each of our clients sites for "their" stuff, and our "main" API that becomes a reposititory of common, or important functions that extend beyond "just theirs". They're all built on the same foundation, so it's quite easy to switch and trade them around as necessary.

    Also, by encoding the results as json, we're able to return many, many data items in a single call (whole tables of columns all at once if need be). I'd recommend looking at ReSTLER, by Luracast, if you want to see who they do it - it's a nice starting point.

    EDIT showing very simnple multi-value return:

    if(is_array($result)) {
        echo json_encode(array_values($result));
    }else{
        echo json_encode($result);
    }
    

    Using Luracast ReSTLer, this code will return everything that PHPInfo contains in a single call:

        ob_start();
        phpinfo($module);
        $info_arr = array();
        $info_lines = explode("
    ", strip_tags(ob_get_clean(), "<tr><td><h2>"));
        $cat = "General";
        foreach($info_lines as $line)
        {
            preg_match("~<h2>(.*)</h2>~", $line, $title) ? $cat = $title[1] : null;
            if(preg_match("~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~", $line, $val))
            {
                $info_arr[$cat][$val[1]] = $val[2];
            }
            elseif(preg_match("~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~", $line, $val))
            {
                $info_arr[$cat][$val[1]] = array("local" => $val[2], "master" => $val[3]);
            }
        }
        return Utilities::arrayToObject($info_arr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答