全局资源URI(即可寻址性)有什么好处?
使用全局唯一URI(就像REST一样)而不是使用专有ID格式来引用资源有什么好处?
例如:
- http://host.com/student/5
- http://host.com/student?id=5
在第一种方法中,整个URL是ID。在第二种方法中,只有5是ID。与第二种方法相比,第一种方法的实际好处是什么?
为什么REST(似乎)竭尽全力提倡第一种方法?
-编辑:
我的问题令人困惑,因为它确实提出了两个独立的问题:
- 可寻址性的好处是什么?
- 上面看到的两种URI形式有什么区别。
我已经用我自己的帖子回答了下面两个问题。
解决方案
搜索引擎优化最多。
在我看来,这也使它们更容易记住,并且更干净,更专业。
首先是在美学上更令人愉悦。
从技术上讲,没有区别,但可以使用前者。
当我看到uri时,主要的事情是普通用户将能够记住该uri。
我们的怪胎可以带问号并获取变量,但是如果有人记得http://www.host.com/users/john而不是http://www.host.com/?view=users&name=john,那么那就是巨大的好处。
我认为这取决于我们要坚持风水原则的程度。
如lafur所述,前一个url的清晰度是一个好处。
另一个是实现的灵活性。
假设学生5很少更改。如果我们使用REST风格的url,则可以选择提供静态文件而不是运行代码。在Rails中,通常对students / 5的第一个请求会在Web根目录下创建一个缓存的html文件。该文件用于处理不涉及后端的后续请求。自然,这种方法没有任何具体的障碍。
后面的网址不允许这样做。静态页面名称中不能包含url变量(?,=)。
从REST的角度来看,这两个URI都是有效的,但是只要意识到Web缓存对待querystring参数的方式就大不相同。
如果我们想利用缓存来发挥自己的优势,那么建议我们不要使用查询字符串参数来标识资源。
我将回答我自己的问题:
1)URI为什么重要?
我将引用伦纳德·理查森(Leonard Richardson)和萨姆·鲁比(Sam Ruby)的RESTful Web服务(ISBN:978-0-596-52926-0):
Consider a real URI that names a resource in the genre “directory of resources about jellyfish”: http://www.google.com/search?q=jellyfish. That jellyfish search is just as much a real URI as http://www.google.com. If HTTP wasn’t addressable, or if the Google search engine wasn’t an addressable web application, I wouldn’t be able to publish that URI in a book. I’d have to tell you: “Open a web connection to google.com, type ‘jellyfish’ in the search box, and click the ‘Google Search’ button. This isn’t an academic worry. Until the mid-1990s, when ftp:// URIs became popular for describing files on FTP sites, people had to write things like: “Start an anonymous FTP session on ftp.example.com. Then change to directory pub/files/ and download file file.txt.” URIs made FTP as addressable as HTTP. Now people just write: “Download ftp:// ftp.example.com/pub/files/file.txt.” The steps are the same, but now they can be carried out by machine. [...] Addressability is one of the best things about web applications. It makes it easy for clients to use web sites in ways the original designers never imagined.
2)可寻址性的好处是什么?
It is far easier to follow server-provided URIs than construct them yourself. This is especially true as resource relationships become too complex to be expressed in simple rules. It's easier to code the logic once in the server than re-implement it in numerous clients. The relationship between resources may change even though the individual resource URIs remain unchanged. For example, if Google Maps were to change the scale of their map tiles, clients that calculate relative tile positions would break.
3)URI比自定义ID有什么好处?
Custom IDs identify a resource uniquely. URIs go a step further by telling you where to find it. This simplifies the client logic.