全局资源 URI(即可寻址性)有什么好处?
与使用专有 id 格式相比,使用全局唯一 URI(如 REST 所做的)引用资源有什么好处?
例如:
在第一种方法中,整个 URL 就是 ID。 在第二种方法中,只有 5 是 ID。 第一种方法相对于第二种方法有什么实际好处?
为什么 REST(似乎)不遗余力地提倡第一种方法?
-- 编辑:
我的问题很混乱,因为它确实提出了两个不同的问题:
- 可寻址性的好处是什么?
- 上面看到的两种 URI 形式有什么区别。
我已经用我自己的帖子回答了下面的两个问题。
What is the benefit of referencing resources using globally-unique URIs (as REST does) versus using a proprietary id format?
For example:
In the first approach the entire URL is the ID. In the second approach only the 5 is the ID. What is the practical benefit of the first approach over the second?
Why does REST (seem to) go out of its way to advocate the first approach?
-- EDIT:
My question was confusing because it really asked two separate questions:
- What is the benefit of addressibility?
- What is the difference between the two URI forms seen above.
I've answered both questions below using my own post.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为这取决于你想要遵守风水原则的程度。
I think it comes down to how closely you want to adhere to the principles of feng shui.
从 REST 角度来看,这两个 URI 都是有效的,但只需意识到 Web 缓存对查询字符串参数的处理方式非常不同。
如果您想利用缓存来发挥自己的优势,那么我建议您不要使用查询字符串参数来标识您的资源。
Both URIs are valid from a REST perspective, however just realize that web caches treat the querystring parameters very differently.
If you want to use caching to your advantage then I suggest that you do not use a query string parameter to identify your resource.
正如 Ólafur 提到的,前一个 url 的清晰度是一个好处。
另一个是实施灵活性。
假设学生 5 很少变化。 如果您使用 REST 样式的 url,您可以选择提供静态文件而不是运行代码。 在 Rails 中,对 Students/5 的第一个请求通常会在您的 Web 根目录下创建一个缓存的 html 文件。 该文件用于服务后续请求,无需接触后端。 当然,这种方法没有任何特定于 Rails 的内容。
后面的 url 不允许这样做。 静态页面的名称中不能包含 url 变量 (?, =)。
As Ólafur mentioned, The clarity of the former url is one benefit.
Another is implementation flexibility.
Let's say that student 5 changes infrequently. If you use the REST-style url you have the option of serving a static file instead of running code. In Rails it is common that the first request to students/5 would create a cached html file under your web root. That file is used to serve subsequent requests w/o touching the backend. Naturally, there's nothing rails specific about that approach.
The later url wouldn't allow for this. You can't have url variables (?, =) in the names of static pages.
第一个更美观。
从技术上讲,没有什么区别,但如果可以的话,请使用前者。
The first is more aesthetically pleasing.
Technically there is no difference, but use the former when you can.
主要是搜索引擎优化。
在我看来,这也让它们更容易记住、更干净、更专业。
Search engine optimization mostly.
It also makes them easier to remember, and cleaner, more professional looking in my opinion.
我将回答我自己的问题:
1)为什么 URI 很重要?
我将引用 Leonard Richardson 和 Sam Ruby 所著的《RESTful Web Services》(ISBN:978-0-596-52926-0):
2)可寻址性有什么好处?
3) URI 相对于自定义 ID 有什么好处?
I will answer my own question:
1) Why are URIs important?
I'll quote from RESTful Web Services by Leonard Richardson and Sam Ruby (ISBN: 978-0-596-52926-0):
2) What is the benefit of addressibility?
3) What is the benefit of URIs over custom IDs?
当我看到这样的 uri 时,最重要的是普通用户能够记住该 uri。
我们这些极客可以接受问号并获取变量,但如果有人记得 http://www.host.com /users/john 而不是 http://www.host.com /?view=users&name=john,那么这是一个巨大的好处。
The main thing when i see uri's like that is a normal user would be able to remember that uri.
Us geeks are fine with question marks and get variables, but if someone remembers http://www.host.com/users/john instead of http://www.host.com/?view=users&name=john, then that is a huge benefit.