PHP 中使用 MVC 的 Post-Redirect-Get (PRG) 最佳实践
PRG 模式与 MVC 是否有最佳实践?
在本教程中:
http://www.theserverside.com/news/1365146/Redirect-After-Post < br> 建议的解决方案需要 4 项行动:
Create_Item(POST)=> “重置”表单并重定向到 Display_Item
Display_Item (GET) =>;显示表单(带有临时数据和错误(如果存在))
Store_Item (POST) =>;尝试将数据保存到数据库,如果错误,保存错误并重定向到 Display_Item,如果成功则重定向到 Display_Stored
Display_Stored (GET) =>;显示创建的项目或成功消息,技术。
现在,我认为使用 POST 进行第一个操作是一个问题,因为我们无法使用链接启动表单。在 Create_Item 中使用 GET 似乎是更好的选择。
而且,我们可以对 3 个操作执行相同的操作(对 Create_Item 和 Display_Item 使用相同的操作,但使用额外的 flag 来重置表单,例如:
http://www.example.com/controller/Create_Item/?reset=1
而且我们只需 2 个操作即可完成相同的操作,因为我们可以在 Create_Item 中使用 if 来检查请求是 GET 还是 POST(因此我们将 Display_Item 与 Store_Item 结合起来)。
我们也可以只用 1 个操作来完成相同的操作,因为我们可以有一个额外的标志(在 URL 查询或会话中)来显示结果而不是表单:
GET http://www.example.com/controller/Create_Item/?reset=1 => ;显示新表单并重定向到下一个 URL
GET http://www.example.com/controller/Create_Item/ =>;显示带有临时数据和错误(如果存在)的表单
POST http://www.example.com/controller/Create_Item/ =>;将错误保存在临时文件中,或将数据保存在数据库中(并设置成功的会话标志)并重定向到上面的 URL 或下一个 URL
GET http://www.example.com/controller/Create_Item/ =>; if $_SESSION['success'] 显示结果
就我个人而言,我喜欢有 4 个操作的想法,但与其他选项相比,我没有任何真正的优势。但如果没有真正的标准,我对选择我的设计感到不安全。
有人知道每种设计的优点和缺点(如果有)吗?
例如,我看到 4 个操作更清晰,但如果我们想更改临时数据的保存方式,我们需要在 4 个地方进行更改。
谢谢!
Is there any best practice for PRG pattern with MVC?
In this tutorial:
http://www.theserverside.com/news/1365146/Redirect-After-Post
the proposed solution requires 4 actions:
Create_Item (POST) => "resets" the form and redirects to Display_Item
Display_Item (GET) => shows the form (with temp data and errors if exists)
Store_Item (POST) => try to save data to DB, if errors, save errors and redirect to Display_Item, if success redirect to Display_Stored
Display_Stored (GET) => shows the item created or a success message, tec.
Now, I think that to have the first action with POST is a problem, because we can't start the form with a link. Using GET in Create_Item seems a better option.
And also, we can do the same with 3 actions (using the same action for Create_Item and Display_Item, but with an extra flag for reseting the form, for example:
http://www.example.com/controller/Create_Item/?reset=1
And also we can do the same with just 2 actions, because we can use an if inside Create_Item for checking if the request is GET or POST (so we are combining Display_Item with Store_Item).
And also we can do the same with just 1 action, because we can have an extra flag (in the URL query or in a session) for showing the results instead of the form:
GET http://www.example.com/controller/Create_Item/?reset=1 => shows a new form and redirects to the next URL
GET http://www.example.com/controller/Create_Item/ => shows a form with temp data and errors if exists
POST http://www.example.com/controller/Create_Item/ => save errors in temp, or data in DB (and set a session flag for success) and redirects to above URL or next URL
GET http://www.example.com/controller/Create_Item/ => if $_SESSION['success'] show results
Personally I like the idea of having 4 actions, but I don't have any real advantage over the others options. But I don't feel secure choosing my design without a real criteria.
Does somebody know the PROS and CONS of each design (if any)?
For example, I see the 4 actions cleaner, but if we want to change how the temp data is saved, we need to change it in 4 places.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该模式是
GET
一个空白表单,修改表单的内容,然后POST
将其发送到服务器,然后服务器将重定向发送到另一个页面,该页面是一个>GET
,可能会显示一个页面,显示表单提交成功。
。 (Get->)Post->Redirect->Get第一个操作并不是真正的
POST
。这就是填写表格并提交的最终结果。该指南更多地介绍了在POST
之后要做什么,就好像您不进行重定向一样,然后用户会留在一个页面上,显示表单提交成功
,他们可以在其中进行操作只需按 F5 并执行另一个POST
即可。然而,通过该重定向,他们可以通过安全的GET
进入该结果页面,这不会导致重复发布。至于实现,您应该让每个操作在服务器端都有自己的操作。这与 MVC/RESTful 实现一致。
如果您想让第二个操作调用
save
,则可以在此处使用 3 个操作。大多数 REST/CRUD 约定都使用 4,但选择权在您手中。其好处与首先采用 REST/MVC 路线相同。另请参阅以下资源:
The pattern is to
GET
a blank form, modify the contents of the form, thenPOST
that to the server, which then sends a redirect to another page which is aGET
, perhaps to a page sayingForm submitted successfully.
. (Get->)Post->Redirect->GetThe first action is not really
POST
. That's the end result of completing a form and submitting it. The guide is more about what to do after thatPOST
, as if you do not do a redirect, then the user is left on a page sayingForm submitted successfully
where they could just hit F5 and do anotherPOST
. With that redirect however, they're on that results page via a safeGET
which will not result in a double post.As for the implementation, you should have each be its own action on the server side. This is inline with the MVC / RESTful implementation.
You could use 3 actions here instead if you wanted to have the 2nd action call
save
. Most REST/CRUD conventions use the 4, but the choice is yours. The benefits are the same as going the REST/MVC route in the first place.See these resources as well: