MVC 中控制器和模型的区别
我对 MVC 框架(codeIgniter)中的控制器和模型有点困惑。我很清楚控制器方法调用视图,模型方法与数据库交互。 但是,我对以下类型的方法有点困惑,这些方法是由控制器中的方法调用的。
hash_password //returns hash password.
valid_email //validates email format and return true or false
is_logged //check if session has a variable, returns true or false
generate_random_string //generates and hashes a random string
它们应该放置在控制器中还是模型中?
目前我将上述所有功能都放在一个控制器中。正确吗?
I'm little confused about controller and model in MVC framework (codeIgniter). Its clear to me that controller methods calls the views and Model methods interact with database.
However, I'm little confused about the following types of methods, which are called by methods in a controller.
hash_password //returns hash password.
valid_email //validates email format and return true or false
is_logged //check if session has a variable, returns true or false
generate_random_string //generates and hashes a random string
Should they be placed in controller or in a model?
Currently I place all of the above functions in a controller. Is it correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为
is_logged
应该放置在User
的模型中。请注意,User
可能是您案例中的客户,也可能是您为服务用户建模而创建的任何类。valid_email
和generate_random_string
或多或少是实用函数,您可以将它们放置在Utility
或Utilities
模型中,以便它们可以在您的应用程序的各种控制器中重复使用。hash_password
可以放置在User
模型或Utility
模型中。我更倾向于将其放入Utility
模型中,因为它是一个哈希函数,并且用户不关心任何内容。然而,我可以想象否则可能会有争论。以下SO问题(尽管针对不同的框架)也可以作为经验法则:
Zend Framework 1.10 中自定义函数的放置位置
I think the
is_logged
should be placed in the Model forUser
. Note that theUser
might be a customer in your case or any class that you have made to model a user of your service.The
valid_email
andgenerate_random_string
are more or less utility functions, which you can place in aUtility
orUtilities
model, so that these are reusable in various controllers in your application.The
hash_password
, can be placed in either theUser
model orUtility
model. I am more tempted to place it inUtility
model, since its a hashing function and there is nothing the user cares about. However, I can imagine there can be argument(s) otherwise.The following SO question (though for a different framework) can also serve as a rule of thumb:
Where to put custom functions in Zend Framework 1.10
通常控制器用于确定如何处理发出的http请求。
创建一些直接响应http请求的函数并没有什么错。
但如果它与数据库有关,最好将这些函数放在模型中,并从控制器调用它们。
generally controllers are used to determine how to handle the http requests made..
There's nothing wrong in creating some functions which directly respond to the http requests.
but if it has anything to do with the DB, its better to place those function in the model, and call them from the controller.
控制器应该将视图与模型结合起来,因此每个验证都应该放在模型中
这是我来自 kohana 的示例
CONTROLLER
MODEL
View 并不那么重要,这就是为什么我不把它放在这里。
Controller should combine view with model, so every validation shoulde be placed in model
this is my example from kohana
CONTROLLER
MODEL
View is not so important thats why i dont put this here.
一般来说 - 模型应该了解它自己的数据。因此,任何纯粹与模型自身数据相关的内容都应该放入模型中。
例如, hash_password 和电子邮件验证方法 - 模型应该知道如何验证或更新它自己的数据字段,因此这些应该放入模型中。
然而,控制器应该知道如何正确地引导用户操作并为视图等加载正确的模型。
例如,与会话相关的方法应该放在控制器中,因为会话用于存储用户的状态(基于过去的操作) 。
“生成随机字符串”方法非常模糊,可能到处都在使用。我会将其放在一个单独的库中,可能酌情包含在模型/控制器中。
Generally speaking - a model should know stuff about it's own data. So anything related purely to a model's own data - should go in the model.
Eg the hash_password and email-validation methods - a model should know how to validate or update it's own data-fields, so those should go in the model.
However a controller should know about how to direct user actions appropriately and to load the correct models for views etc.
EG the session-related method should go in the controller, because the session is used for storing the user's state (based on past actions).
The "generate random string" method is very vague and may be used everywhere. I'd put that in a separate library possibly included in the model/controller as appropriate.
我已经使用 Codeigniter 很长时间了,就放置而言,我会使用您的函数执行以下操作:
我将密码哈希器之类的东西放在库或帮助程序文件中,这样我就可以从我的控制器中调用它,例如:
我假设您想对密码进行哈希/盐处理并将其存储在该示例中的数据库中
这已经在 form_validation 中,所以...
这也应该连接到身份验证库
同样,这将来自库或助手。
那么什么时候使用模型呢?
我,我专门使用模型进行数据库的输入/输出。我所有的疑问都在那里。我通常让模型的函数返回数据对象,这样我就可以在视图中循环遍历它们。
控制器从模型中调用数据,然后将所有内容转储到视图中。外部功能总是进入库和助手中。我喜欢做“MY_library”并扩展 Codeigniter 自己的东西 - 特别是表单和 html 帮助器等。
I've been using Codeigniter for a long time and I'd do the following with your functions as far as placement goes:
I'd put something like a password hasher in a library or helper file so I could call it from my controller like:
I'm assuming you want to hash/salt the password and store it in your database in that example
This is already in form_validation, so...
This should also connect to a authentication library
Again, this would come from a library or helper.
SO WHEN DO YOU USE A MODEL?
Me, I use models exclusively for in/out on the database. All my queries go in there. I usually have my model's functions return data objects so I can loop through them in my views.
Controllers call your data from your models, then dump everything into your views. Outside functionality always goes into libraries and helpers. I like to do the "MY_library" and extend Codeigniter's own stuff - especially with forms and the html helper etc.