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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?