在 RESTful API 中使用干净的 URL [英] Using Clean URLs in RESTful API

查看:20
本文介绍了在 RESTful API 中使用干净的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

干净的 URL"也称为RESTful URL"是用户友好的、纯结构的,并且不包含查询字符串.相反,它们只包含资源的路径.

"Clean URLs" also known as "RESTful URLs" are user-friendly, purely structural, and do not contain a query string. Instead they contain only the path of the resource.

即:http://twitter.com/users/show/"+用户名+.json"

ie: "http://twitter.com/users/show/"+username+".json"

关于服务器端功能的问题:

  1. 我是否需要为每个目录制作一个唯一的服务器端 API 脚本?

  1. Do I need to make a unique server-side API script for each directory?

如果可以,我可以将所有请求转发给一个脚本吗?从 Clean URL 结构 ($_GET['url_structure']) 中提取有用信息?

Can I forward all requests to just one script if so how do I pull useful information from the Clean URL structure ($_GET['url_structure'])?

为什么 twitter 调用 .json 文件那肯定不存在.它必须根据要求生成.如何这行得通吗?这让我相信问题的答案2 是.

Why does twitter call to a .json file that surely doesn't exist. It must get generated on request. How does this work? This leads me to believe that the answer to question 2 is yes.

推荐答案

1) 如果您使用像 RecessPHP 或者如果您在 .htaccess 文件中使用 mod_rewrite 规则将所有 API 请求重定向到单个 PHP 文件(称为前端控制器).

1) Not if you use a RESTful framework like RecessPHP or if you use a mod_rewrite rule in your .htaccess file to redirect all API requests to a single PHP file (known as the front controller).

.htaccess

RewriteEngine On
RewriteRule ^/api/ api.php

api.php

$request = $_SERVER['REQUEST_URI'];  //this would be /users/show/abc.json

<小时>

2) 您可以使用 apache 的重写模块将所有 api 请求重定向到处理它们的特殊 PHP 文件.根据您的 apache 配置,原始请求的 (RESTful) url 将存储在 PHP 中的服务器变量中,我相信它是 $_SERVER['REQUEST_URI'].当然,您也可以将 $_GET[] 变量传递给包含 RESTful url 的 PHP.


2) You can use the rewrite module of apache to redirect all api requests to a special PHP file that handles them. Depending on your apache configuration, the original requested (RESTful) url will be stored in a server variable in PHP, I believe it's $_SERVER['REQUEST_URI']. Of course you could also just pass along a $_GET[] variable to PHP that contained the RESTful url.

.htaccess

RewriteEngine On
RewriteRule ^/api/([^.]+).(xml|json|atom) api.php?url=$1&type=$2

api.php

$request_parts = explode('/', $_GET['url']); // array('users', 'show', 'abc')
$file_type     = $_GET['type'];

$output = get_data_from_db(); //Do your processing here
                              //You can outsource to other files via an include/require

//Output based on request
switch($file_type) {
    case 'json':
        echo json_encode($output);
        break;
    case 'xml':
        echo xml_encode($output); //This isn't a real function, but you can make one
        break;
    default:
        echo $output;
}

<小时>

3) Twitter(和许多其他 API)使用它是因为它是一种提供应用程序期望从 API 返回的格式的便捷方式.所有 API 请求都重新路由到单个 PHP 文件,该文件处理创建所有文件并将其内容回显到输出.该文件从未真正存储在服务器上(除非它被缓存).


3) Twitter (and many other APIs) use this because it is a convenient way of supplying the format that an application expects back from an API. All of the API requests are rerouted to a single PHP file which handles creating all the files and echoing their contents to the output. The file is never actually stored on the server (unless it is cached).

好资源

关于 RecessPHP 的说明.它当然是一个很棒的工具,我鼓励您查看它(也许可以从它的源头了解它如何处理事物),但话虽如此,对我来说似乎有点笨拙.路径名写在特殊注释中的事实对我来说似乎非常不 PHP.我会偏离这一点,我不会称它为完美的框架,但这肯定是一个开始.祝你好运!

A note on RecessPHP. It's certainly a great tool and I would encourage you look at it (maybe at its source to get an idea of how it processes things), but that said, it seems a bit clunky to me. The fact that path names are written in special comments seems very not-PHP to me. I'd stray away from this, and I wouldn't call it the perfect framework, but it's certainly a start. Good luck!

这篇关于在 RESTful API 中使用干净的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆