MVC 框架中如何使用表单?
我已经做简单的 MVC 教程有一段时间了,我明白了这个概念。但我想知道,当表单显示在视图中时,表单是如何处理的?检查表单提交的代码必须存在于视图文件中,这并不真正适合,因为视图应该仅用于显示输出。
那么,当 MVC 框架的视图文件中有一个表单时,检查表单提交的代码应该在哪里呢?
i have been doing simple MVC tutorials for a while and i get the concept. But i am wondering, when a form is displayed in a view, how is the form processed? A code to check for form submission must be present in the view file, which doesn't really fit because view should just be for displaying output.
So when you have a form in a view file of an MVC framework, where should the code to check for form submission be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当然,这取决于具体的框架,但这是相当典型的:
Of course this depends on the specific framework, but this is rather typical:
表单提交可以在控制器中处理。检查这个
The form submission can be handle in controller. check this
检查和验证表单提交的代码应该位于控制器或模型中,具体取决于从表单接收的数据类型以及您正在使用它执行的操作。这就是 MVC 的要点。视图文件应该只包含显示页面所需的最少量的逻辑。
The code to check for and validate the form submission should be in the controller or the model, depending upon the type of data received from the form and what you're doing with it. That's the point of MVC. View files should contain only the barest amount of logic necessary to display the page.
我认为最常见的方法是控制器,因为它是控制器处理所有输入数据(通过 $_POST、$_GET 等),然后最终决定调用哪些方法来处理该输入以及输出哪个视图。
I think the most common approach would be the controller, since it is the controller that handles all input data (via $_POST, $_GET etc) and then ultimately decides which methods to call to handle that input, and which view to output.