PHP表单处理-分布式还是集中式? [英] PHP Form Processing - Distributed or Centralised?

查看:89
本文介绍了PHP表单处理-分布式还是集中式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了搜索,但是似乎没有一个更好的共识.当前,该站点上的所有HTML表单都指向一个PHP文件.每个表单都有一个隐藏的输入来指定操作(例如'user-login','user-logout'),然后PHP文件从中调用方法.

I've searched around but there doesn't seem to be a clear consensus on which one is better. Currently, all the HTML forms on the site point to a single PHP file. Each form has a hidden input specifying the action (e.g. 'user-login', 'user-logout'), and the PHP file calls methods from that.

所以我的问题是:我应该将每个表单都指向自身,将相关表单指向单个文件还是将 all 表单指向单个文件?而且就MVC而言,处理应该在控制器或表单中进行吗?

So my question is: Should I point each form back to itself, related forms to a single file or all forms to a single file? And in terms of MVC, should processing take place in the controller or form?

推荐答案

我应该将每个表单都指向自身,将相关表单指向单个文件还是将所有表单指向单个文件?

Should I point each form back to itself, related forms to a single file or all forms to a single file?

您应将所有内容都指向index.php,然后委派其他组件(MVC术语为控制器)来处理.然后,控制器将决定要呈现哪个 view .在这种情况下,我们将index.php称为前端控制器. ( note :下面,我推荐ZF1作为学习平台.ZF1具有前端控制器"类.有人可能会争辩说,一个是前端控制器,而index.php是他们所谓的输入脚本.在我看来,这只是第二个前端"控制器.尽管如此,两种观点都存在争议,因此请发表您自己的观点.

You should point everything to index.php, which then delegates other components (controllers in MVC terms) to take care of the processing. The controller will then decide which view it wants to render. index.php would be in this case something we call the front controller. (note: below I am recommending ZF1 as a learning platform. ZF1 has a "front controller" class. Some people may argue that THAT one is the front controller and index.php is what they call the entry script. In my opinion, that is only the second "front" controller. Nevertheless, both opinions are controversed, so make your own opinion).

就MVC而言,应该在控制器或表单中进行处理吗?

And in terms of MVC, should processing take place in the controller or form?

最重要的是面向对象操作:对象是唯一知道如何验证其自身数据(自包含原理)的对象,因此该表单应进行自身验证.如果是关于模型的,则模型应该由控制器或表单来调用-这是一个品味问题.无论采用哪种方式,都适用相同的原则:向模型提供数据并调用验证方法.

In terms of OOP first and foremost: the object is the only one who knows how to validate its own data (the principle of self-containment), so the form should validate itself. If it's about a model, the model should be called by either the controller, or the form - it's a matter of taste. No matter which way, the same principle applies: you feed the Model with data and call its validation method.

您可能已经注意到,Form类是Model.

As you may have noticed, the Form class is a Model.

不要让自己被称为MVC的炒作所骗. 首先要尊重OOP原则.

Don't let yourself fooled by the hype called MVC. Respect the OOP principles above all.

关于MVC:MVC模式说:控制器仅协调其他组件,例如,它接受输入,创建Form实例并调用Form的验证方法.

Regarding MVC: the MVC pattern says: the controller only coordonates the other components, for instance it takes the input, creates a Form instance, and calls the Form's validation method.

我建议您使用一个框架来更好地了解所有这些部分如何协同工作.最好的选择是zend framework 1,它与现实生活的要求无关,而BUT在模式和实践方面都是杰作.

I advise you to use a framework to better see how all these pieces work together. The best would be zend framework 1, which has little to do with real life requirements, BUT which is a masterpiece in terms of patterns and practices.

也许ZF2会使用额外的前端控制器来解决该错误.

Maybe ZF2 will change that mistake with the extra front controller.

看看其他答案,我觉得有必要澄清我的答案中使用的一些术语:

Looking at the other answers, I feel the need to clarify some terminology used in my answer:

  • Model是一个模型.有很多关于MVC的文档
  • FormModel的子类.它负责验证.它还可能具有方法Form::__toString(),该方法可在 view (MVC中的V)
  • 中呈现HTML表单.
  • view 是html文件,在 controller (在MVC中为C)的监视下呈现.
  • Model is a model. There's plenty of documentation about MVC
  • Form is a subclass of Model. It takes care of the validation. It may also have a method Form::__toString() which renders the HTML form in the view (the V in MVC)
  • view is the html file, and it's rendered under the supervision of the controller (C in MVC)

回顾一下,整个执行流程如下:

To recap, the overall execution flow would look like this:

  1. <form action ...
  2. 输入脚本(前端控制器)
  3. 路由器(它决定将请求转发到哪个控制器)
  4. Controller协调所有动作,调用一个或多个模型(包括Form,它也是Model)的Model::validate()
  5. 最后,Controller选择一个render()视图(一个html文件),该视图可能包含对Form::__toString()的调用,在这种情况下,FormModel和渲染器".
  6. 好玩
  7. 利润
  1. <form action ...
  2. entry script (a front controller)
  3. router (it decides which Controller to forward the request to)
  4. the Controller coordinates all the actions, calling the Model::validate() of one or many models (including Form, which is also a Model)
  5. in the end, the Controller choses to render() a view (a html file), which could contain a call to Form::__toString(), in which case the Form is a hybrid of Model and "renderer".
  6. Fun
  7. Profit

就是这样,基本上.不同的框架具有不同的数据/执行流程. ZF1的示例如下: http://www.slideshare.net/polleywong/zend-framework-dispatch-workflow

That's it, basically. Different frameworks have different data/execution flow. ZF1's looks like this for instance: http://www.slideshare.net/polleywong/zend-framework-dispatch-workflow

这篇关于PHP表单处理-分布式还是集中式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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