PHP 中使用 MVC 的 Post-Redirect-Get (PRG) 最佳实践

发布于 2024-11-06 03:54:16 字数 1813 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2024-11-13 03:54:16

该模式是 GET 一个空白表单,修改表单的内容,然后 POST 将其发送到服务器,然后服务器将重定向发送到另一个页面,该页面是一个 >GET,可能会显示一个页面,显示表单提交成功。。 (Get->)Post->Redirect->Get

第一个操作并不是真正的POST。这就是填写表格并提交的最终结果。该指南更多地介绍了在POST之后要做什么,就好像您不进行重定向一样,然后用户会留在一个页面上,显示表单提交成功,他们可以在其中进行操作只需按 F5 并执行另一个 POST 即可。然而,通过该重定向,他们可以通过安全的 GET 进入该结果页面,这不会导致重复发布。

至于实现,您应该让每个操作在服务器端都有自己的操作。这与 MVC/RESTful 实现一致。

  • 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 路线相同。

另请参阅以下资源:

  • RESTful Web 服务
  • 涵盖了 RESTful 控制器的典型约定。它涵盖了 Rails,但如果您想采用 REST 路线,也仍然适用于 PHP。

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

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.

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 -> Call new_form() method to render a new form
  • POST /url?action=create -> Call create_form() method to save and redirect to /url?action=show&id=1234
  • GET /url?action=show&id=1234 -> Call show_form() method to display the result
  • POST /url?action=save&id=1234 -> Call save_form() method to save and redirect

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:

  • 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文