我应该合并我的创建和更新控制器吗?
我用于更新和创建对象的控制器非常相似 - 它们都执行相同的表单验证和重定向。但他们当然会调用模型中的不同方法。我应该合并它们还是将它们分开?或者我应该为验证创建一个单独的辅助函数?
优点:
- 一个控制器而不是两个
- 没有重复的验证代码
- 视图(我合并的)始终可以指向同一个控制器
缺点:
- 合并的控制器更混乱 - 那里有几个地方我必须检查它是更新还是创建。
My controllers for updating and creating an object are fairly similar - they both do the same form validation and redirect. But they do, of course, call different methods in the model. Should I merge them or keep them separate? Or should I just make a separate helper function for the validation?
Pros:
- One controller instead of two
- No duplication of validation code
- The view (which I did merge) can always point to the same controller
Cons:
- The merged controller is messier - there are a couple places where I have to check whether it's an update or a create.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如何创建一个验证方法,传递一个表示模式(添加或更新)的变量,并让它在验证后调用相应的控制器?
How about creating a validation method that is passed a variable that would denote the mode (add or update) and have it call the corresponding controller after validation?
我不会合并两个控制器(我喜欢在那里有一些统一性),要保持干燥(这确实很重要)并保持控制器的方法精简,我宁愿创建一个用于公共验证的方法。
I would not merge the two controllers (I like to have some uniformity there), to be DRY (that it is indeed important) and to keep controllers' method skinny I would rather create a method for the common validation.
更新和插入是两个不同的事情。
更新将返回受影响的行数,插入将返回该插入的 ID。这很重要,因为您应该在返回控制器功能之前检查更新或插入是否实际发生。
我应该补充一点,验证不受影响。无论是插入还是更新,您都可以调用验证函数。如果验证失败,您将重新显示表单。如果成功,您就可以进行更新或插入。
Update and insert are two different things.
Update will return the number of rows affected and insert will return the id of that insert. that's important since you should be checking if the update or insert actually happened before returning to the controller function.
I should add that validation isn't affected. You call the validation function regardless of an insert or update. If the validation fails you redisplay the form. If it succeeds you do your update or insert.
我不会有不同的控制器,只有同一控制器中的不同方法。
控制器将是“blog.php”,那么更新是一种方法,插入是另一种方法。
例如:
通过这种方式,您可以在控制器属性中设置验证并仅使用一次。
如果您基于帖子或从数据库中提取创建 $data['blog'] 数组,您甚至可以使用相同的形式。这可能有点太过分了,但在某些情况下这是有意义的。
I wouldn't have a different controller, only different methods in the same controller.
A Controller would be "blog.php" then update is one method and insert is another.
For example:
This way you can set validation in the controller properties and use it just the once.
You can even use the same form if you create a $data['blog'] array based on post or pulled from the db. That might be a step too far, but it makes sense in some situations.
我几乎总是合并它们。相同的验证、相同的前端页面指针等。您必须执行一些检查,但我认为它们超过了缺点。
当需要添加字段或修改处理程序的某些逻辑时,就容易得多。
I almost always merge them. Same validation, same front end page pointers, etc. There are a few checks you have to do, but I think they outweigh the downside.
And when it comes time to add fields or modify some logic of the handlers, it is a lot easier to do.
对于同一控制器,添加和更新是不同的方法。尽管我非常喜欢保持轻量级和聪明的代码,但归根结底,混合东西更多的是麻烦而不是解决方案。
我想这取决于您提交的表格的复杂程度。例如,当您必须验证图像替换时,这可能会很痛苦。
MVC 就是让事情变得小而简单,对吗?为什么要混合起来呢?
Add and Update are diferent methods for the same controller. Although I'm a big fan of keeping light and clever code, at the end of the day, mixing stuff is more a trouble than a solution.
I suppose it depends on the complexity of the form you're submitting. When you have to validate image replacements, for example, it can be a pain.
MVC is about keeping things small and simple, right? Why mix it up?