MVC Design - How many controllers can/should/must

2020-02-26 06:30发布

I'm building a relatively simple web application in PHP using the CodeIgniter MVC framework. I've developed PHP applications before but never in a disciplined manner. I'm getting used to the MVC framework, however two questions keep bringing me back to the drawing board and it's slowing down development.

  1. Is there a best practice on how many controllers a web app can have? I have one (fairly large) controller for my app that includes business logic, form submission logic, etc. While this works, I'm debating whether it would make sense to have separate controllers, e.g. one for form handling and submission, another for user management (session, login, sign up), etc. Then again, I don't want to overengineer the heck out of this either. The part where I am getting hung up is: the controller has a "default" index function that gets loaded at the parent URL. All the business logic starts off from here (e.g., form submission, doing something with the data, etc.). I've defaulted to just creating new functions in the same controller as development continued. It works, but part of my goal is to also learn the best practice and something tells me I may be going about this the wrong way.

  2. Should models only be used for database read/write functions or can I put "helper" functions in there as well, e.g. generate_random_number, validate_login_credentials, login_session, logout_session, etc.

2条回答
2楼-- · 2020-02-26 07:14

(This is my subjective opinion - it has treated me well)

Models should be the meat and bone of your entire applications. The models should handle all business logic and database management. Meanwhile, the controllers should be as thin as possible, only really providing an interface between the model and view.

For instance, in a login screen, the controller should provide the user with the login view. When the user inputs his information, the controller should handle input validation and forward the input to the model, which should respond with "success" or "failure". Consequently the controller should redirect the user to the dashboard, or send him back to the login screen with an error message - respectively.

To summarize: Models should be fat, controllers should be thin.

查看更多
▲ chillily
3楼-- · 2020-02-26 07:18

Controllers

It all depends upon the nature of the application, but in general the answer is NO you should not have "one fairly large controller".

The more you break an application up into smaller pieces, the easier it is to maintain.

Models

Directly from the Codeigniter docs

Models are PHP classes that are designed to work with information in your database.

The answer is yes, you should only use models for data interaction.

I think it's funny you actually answered yourself

"... or can I put 'helper' functions in there as well, ..."

It happens Codeigniter has a facility that handles this type of functionality...

Codeigniter Helpers

查看更多
登录 后发表回答