在PHP中使用MVC进行事后重定向获取(PRG)的最佳做法 [英] Best practices for Post-Redirect-Get (PRG) with MVC in PHP

查看:155
本文介绍了在PHP中使用MVC进行事后重定向获取(PRG)的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MVC进行PRG模式是否有最佳实践?
在本教程中:
http://www.theserverside.com/news/1365146/Redirect-After-Post
提议的解决方案需要4个操作:
Create_Item (POST)=>重置"表单并重定向到Display_Item
Display_Item (GET)=>显示表单(带有临时数据和错误,如果存在)
Store_Item (POST)=>尝试将数据保存到DB,如果出错,则保存错误并重定向到Display_Item,如果成功,则重定向到Display_Stored
Display_Stored (GET)=>显示创建的项目或成功消息,tec.

现在,我认为对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/ =>将错误保存在临时文件中,或将数据保存在DB(并设置成功会话标志)并重定向到上面的URL或下一个URL
如果$ _SESSION ['success'],则获取 http://www.example.com/controller/Create_Item/ =>显示结果

就个人而言,我喜欢执行4个动作的想法,但是与其他选项相比,我没有任何真正的优势.但是,如果没有真正的标准,我就不会选择我的设计.
有人知道每种设计的 PROS CONS (如果有)吗?

例如,我看到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!

推荐答案

模式是GET一个空白表单,修改表单的内容,然后POST将该表单发送到服务器,然后服务器将重定向发送到另一个页面是GET,也许是说Form submitted successfully.的页面. (获取->发布->重定向->获取

The pattern is to GET a blank form, modify the contents of the form, then POST that to the server, which then sends a redirect to another page which is a GET, perhaps to a page saying Form submitted successfully.. (Get->)Post->Redirect->Get

第一个动作实际上不是POST.那是完成表格并提交的最终结果.该指南更多地是关于POST之后的操作,就好像您不执行重定向一样,该用户也留在说Form submitted successfully的页面上,他们可以按F5并执行另一个POST.但是,使用该重定向后,他们就会通过安全的GET进入该结果页面,而不会产生重复的帖子.

The 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 that POST, as if you do not do a redirect, then the user is left on a page saying Form submitted successfully where they could just hit F5 and do another POST. With that redirect however, they're on that results page via a safe GET which will not result in a double post.

对于实现,您应该在服务器端分别执行各自的操作.这与MVC/RESTful实现是一致的.

As for the implementation, you should have each be its own action on the server side. This is inline with the MVC / RESTful implementation.

  • GET/url?action = new->调用new_form()方法呈现新表单
  • POST/url?action = create->调用create_form()方法保存并重定向到/url?action = show& id = 1234
  • GET/url?action = show& id = 1234->调用show_form()方法以显示结果
  • POST/url?action = save& id = 1234->调用save_form()方法进行保存和重定向

如果要调用第二个操作save,则可以在此处使用3个操作.大多数REST/CRUD约定都使用4,但是选择是您自己选择的.好处与首先使用REST/MVC路由相同.

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.

也请参阅以下资源:

  • RESTful Web服务
  • 涵盖了RESTful控制器的典型约定.它涵盖了rails,但是如果您想走REST路线,它仍然同样适用于PHP.
  • RESTful web services
  • This covers typical conventions for RESTful controllers. It covers rails, but still applies to PHP as well if you're wanting to go the REST route.

这篇关于在PHP中使用MVC进行事后重定向获取(PRG)的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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