是仅适用于 Web 服务还是适用于 Web 服务和网页? [英] is restful meant for web services only OR for both web services AND web pages?

查看:36
本文介绍了是仅适用于 Web 服务还是适用于 Web 服务和网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了很多关于 PHP 的 Restful 教程.

I read a lot of Restful tutorials for PHP.

(我不想深入探讨为什么我不使用 RoR.这是因为团队更熟悉 PHP)

(I don't want to go in depth into why I am not using RoR. It is due to the team being more familiar with PHP)

因为我们计划未来扩展到拥有 API,所以我读到实现 Restful Web 服务很重要.

Because we are planning for future expansion into having APIs i read that it is important to implement Restful web services.

我看过诸如

http://www.gen-x-design.com/archives/create-a-rest-api-with-php/

显然,restful 适用于网络服务.

Apparently restful is meant for webservices.

网页呢?它们也可以 RESTFUL 吗?

What about for webpages? can they be RESTFUL as well?

如果答案是否定的,请不要越界,直接告诉我.谢谢.

if the answer is NO, please do not go beyond this line AND just tell me. Thank you.

我知道让 url 看起来像 RESTFUL url 只需使用 mod_rewrite.然而,我很确定,restful 架构不仅仅是让 url 看起来不错.

i know to make the urls look like RESTFUL urls is to simply use mod_rewrite. However, i am quite sure, restful architecture goes beyond just making the urls look nice.

例如,我在名为 list.php 的网页上有一个项目列表.每个项目旁边都有一个删除链接.例如,list.php?id=1&deleteitem

For eg, i have a list of items on a webpage called list.php . Each item has a delete link next to it. E.g., list.php?id=1&deleteitem

当有人点击 list.php?id=1&deleteitem 链接时,我当然会回到同一个 list.php 文件并检查 $_GET 中的参数 deleteitem 时会发生什么.

What happens is that when someone clicks on the list.php?id=1&deleteitem link, of course i go back to the same list.php file and check for the param deleteitem in $_GET.

如果检测到,我将根据 $_GET 中的参数 id 从数据库中删除.

If detected, i will then delete from database based on the param id in $_GET.

之后我将重定向回 l​​ist.php 而不带任何参数.

After which i will redirect back to list.php WITHOUT any params.

我想知道,我如何使整个流程变得 RESTFUL?

I am wondering, how do i make this whole flow RESTFUL?

我之所以这么问是因为在 REST 中,要删除某些内容,您需要使用 HTTP 请求方法 (DELETE).

I am asking because in REST, to make delete something you need to use HTTP request method (DELETE).

很明显,在我的链接中,它们都只是简单的 <a href="list.php?id=1&deleteitem">Delete</a>

Clearly in my links they are all just simply <a href="list.php?id=1&deleteitem">Delete</a>

请赐教.

我的编程不是那么强,如果给出的建议尽可能通俗易懂就好了.

My programming is not that strong and it would be good if the advice given can be as layman as possible.

谢谢.

编辑

我有 2 个后续问题.

I have 2 follow up questions.

问题 1) 由于这是一个带分页的项目列表,如果我想让它成为 RESTful,URL 会是什么样子?

question 1) Since this is a list of items with paging, how would the URL look like if i want to make it RESTful?

问题 2) 由于我将 DELETE 链接放在列表中的每一项旁边,我现在明白了,我应该使用

question 2) Since i am putting DELETE links next to every one of the items in the list, I understand now, i should use

<form method="POST">
<input type=hidden name="_method" value="delete">
<input type="submit" >
</form>

相反.

不过表格应该贴到哪里?项目网址?/items/{item-id}

however the form should be posted to where? the item url? /items/{item-id}

但我想返回此列表页面,在成功删除数据库中的行后显示成功消息.

But i want to go back to this listing page displaying a success message AFTER successfully deleting the row in database.

当我用成功消息刷新此列表页面时,我还想避免弹出消息.

I also want to avoid a popup message when i refresh this listing page with success message.

如果我发回这个 list.php url,那么它不是 RESTful 是吗?因为下面的答案告诉我,每个项目都是需要自己 url 的资源.

If i post back to this list.php url, then it is not RESTful yes? because i am told by the answers below that each item is a resource which needs its own url.

请赐教.谢谢.

推荐答案

RESTful 通常用于 Web 服务,但它可以很好地应用于网页.简而言之,RESTful 就是处理资源.资源可以是一个人、一本书、一部电影、一个剧院、一张票或任何你喜欢的东西.

RESTful is commonly used when referring to web services, but it can very well apply to web pages. In a nutshell, being RESTful is about dealing with resources. A resource can be a person, a book, a movie, a theatre, a ticket, or whatever you fancy.

您可以对资源执行四种基本操作.

There are four basic operations you could perform on a resource.

  • 创建(POST)
  • 读取(获取)
  • 更新(PUT)
  • 删除(删除)

大多数网络浏览器不支持 PUTDELETE 操作,因此您只能使用 POST 操作发送数据.Rails 通过传入一个名为 _method 的隐藏参数来伪造 PUT 和 DELETE,框架会根据该值选择并路由它.

Most web browsers do not support the PUT and DELETE actions, so you can only use POST actions for sending data. Rails fakes PUT and DELETE by passing in a hidden parameter named _method that the framework picks up and routes it depending on that value.

此外,您永远不应该将 GET 用于任何破坏性操作.任何更改资源状态的操作都应使用 POSTPUTDELETE 调用,具体取决于更改(如果需要,可以使用 POST 伪造 PUT/DELETE).

Also, you should never use GET for any destructive action. Any action that changes the state of your resource(s), should be called with either POST, PUT, or DELETE depending on the change (fake PUT/DELETE with POST if need be).

我建议您查看 Rails 中处理 RESTful 路由的方式,以获得一个想法,如果没有别的.尽管上述四个操作足以以任何可能的方式修改资源,但 Rails 还引入了其他三种听起来很有用的操作.

I would suggest you checkout the way RESTful routing is handled in Rails just to get an idea, if nothing else. Althought the four actions above are sufficient to modify a resource in any way possible, Rails also introduces three other types of actions that sound useful.

  • index(所有项目的列表视图)
  • new(通常是一个表单视图来添加一个新的资源)
  • 编辑(通常是表单视图更新现有资源)

在设计 RESTful 站点时,Pretty URL 肯定会摆在桌面上,但最大的好处可能是代码质量会自动提高.当您只处理资源,并且只有四种可能的操作可以应用于资源时,事情就会开始自行清理.

Pretty URL's is definitely on the table when designing RESTful sites, but probably the biggest win is that the quality of code improves automatically. When you're only dealing with resources, and there are only four potential actions that can be applied to a resource, then things start to clean up by themselves.

编辑 1:自描述 URL 是首选,这将使您的生活更轻松,但创建唯一标识资源并使用 HTTP 动词对其进行管理的神秘 URL 无所不能.像下面这样(使用 md5)来唯一标识资源的 URL 是完美的 RESTful.

Edit 1: Self-descriptive URL's are preferred and will make your life easier, but there's nothing stopping from creating cryptic URLs that uniquely identify a resource and manage it using the HTTP verbs. URLs such as the ones below (using md5) to uniquely identify resources are perfectly RESTful.

// represents a person "John Doe"
http://example.com/4c2a904bafba06591225113ad17b5cec

// represents the movie "Lord of the Rings: The Two Towers"
http://example.com/1d9198260dec7bda3fb21e232d60fec3

// represents the "Formula One" sport
http://example.com/fs340as?id=23xa12&type=SP012Ts

这就是代表性状态转移.MD5 哈希值就像保持不变的资源的邮寄地址.表示可能是电影的详细信息(在 html/xml/json/etc. 中),或者可能是电影本身的视频,具体取决于客户端的功能).

That's REpresentational State Transfer right there. The MD5 hash is like the mailing address of the resource that remains constant. The representation could be the movie's details (in html/xml/json/etc.), or maybe the video of the movie itself depending on the capabilities of the client).

编辑 2:假设您有一个资源,它是一个集合 - countries 世界.它可以由 URI 和 HTTP 动词表示,例如:

Edit 2: Let's say you have a resource which is a collection - countries of the world. It could be represented by a URI and HTTP verb such as:

GET /countries

由于分页是应用程序的属性而不是资源本身,因此您可以提供查询字符串参数来控制它.

Since paging is a property of the application rather than the resource itself, you could supply querystring parameters to control it.

GET /countries?page=1

国家也是国家资源的一部分的资源.您可以使用 URL 来标识国家/地区,例如:

A country is also a resource that is a part of the countries resource. You could identify a country with a URL such as:

/countries/<id>

可以用这些动词在这个国家进行操作:

And operations on this country could be performed with these verbs:

GET    /countries/<id>
POST   /countries  -- the new country has no existing representation
PUT    /countries/<id>
DELETE /countries/<id>

这篇关于是仅适用于 Web 服务还是适用于 Web 服务和网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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