联系页面 MVC 工作流程
我以前的所有项目都在联系页面上有此工作流程
- 用户提交表单
- 控制器获取
$_POST
详细信息 - 控制器验证详细信息(并在必要时设置错误消息)
- 控制器发送电子邮件
- 控制器重定向到谢谢 页
这是标准工作流程吗?
我曾经验证控制器中的所有内容,然后做了更多阅读,他们建议不要这样做。因此,我应该将 $_POST
详细信息发送到辅助类型对象并让它完成所有工作(验证/发送)吗?
All my previous projects have had this workflow on Contact pages
- User submits form
- Controller gets
$_POST
details - Controller validates details (and sets error messages if necessary)
- Controller sends email
- Controller redirects to thanks page
Is this the standard workflow?
I used to validate everything in controllers, and then did some more reading and they recommended against it. Therefore, should I send the $_POST
details to a helper type object and let it do all the work (validation/sending)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在控制器中我们应该只检查验证。主要验证应该在数据库操作之前对模型进行。
In controller we should only check validation. The main validation should be on model before operations with DB.
控制器文件需要检查&验证用户输入的数据。
得到 & 之后积累所有数据后,需要将数据传输到模型文件以与数据库进行检查(如果需要)&然后需要从这里做一些其他工作(例如设置会话/cookie,或发送邮件,或触发挂钩,...)。但是,控件必须返回到相同的控制器方法,因为所有先前的模型功能都必须通过来自相同控制器方法的方法调用来触发。
现在必须调用正确的视图方法,然后必须将输出呈现到控制台。
希望有帮助。
The controller file need to check & validate the user input data.
After getting & accumulating all the data, it needs to transfer the data to the Model file for checking with the database (if needed) & then need to do some other works from here (like setting sessions / cookies, or sending mails, or firing hooks, ...). However, the control must come back to the same controller method, as all the previous model functionalities must be fired by a method call, from the same controller method.
The proper view method must be called now, and then the output must be rendered to the console.
Hope it helps.
验证通常在
模型
中执行,而不是在控制器
中执行。这是因为数据结构通常在模型中定义,最好在操作(即插入数据库等)之前立即比较获取的数据。
Validation is typically performed in the
Model
, not in theController
.This is because data structures are usually defined in the
Model
and it is best to compare the acquired data immediately before manipulation (i.e. inserting into a database, etc.).