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?
I think the
is_loggedshould be placed in the Model forUser. Note that theUsermight be a customer in your case or any class that you have made to model a user of your service.The
valid_emailandgenerate_random_stringare more or less utility functions, which you can place in aUtilityorUtilitiesmodel, so that these are reusable in various controllers in your application.The
hash_password, can be placed in either theUsermodel orUtilitymodel. I am more tempted to place it inUtilitymodel, 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