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

查看:187
本文介绍了在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网址的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).

良好的资源

  • mod_rewrite, a beginner's guide
  • Create a REST API with PHP
  • Recess PHP Framework | Tutorials

关于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天全站免登陆