MVC: Variables used for only for user-registration. Placed in Controller helper or Model?

a 夏天 提交于 2019-12-09 08:34:28

I would create a register method in your User model, and pass the required info to it from the controller, since that is the part of your application that handles user input. Reusing models tends to happen more than reusing controllers, so by doing this you could potentially save yourself some time during a future project (just copy over your User model.)

Another benefit is that you can now use this registration logic from multiple points in your application. Granted, user registration isn't the best example for this, but I hope you see how this could be useful in other situations.

A phrase I like to remember when I find myself in a similar situation is "Skinny controller, fat model". Try to keep your controllers slim, and feel free to fatten up those models! :)

Edit: Here's some pseudo-code to help explain what I mean...

class RegistrationController {
  function register() {
      // Sanitizing your data here would be a good idea
      $fieldArr = $_POST['data_from_your_form']; 

      $user = new User();
      $result = $user->register($fieldArr);

      if ($result) {
          // User successfully reg'd
      } else {
          // Oops! Problem registering user
      }
  }  
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!