Alex Rivera | Logout

MVC Design - How many controllers can/should/must I have in a CodeIgniter MVC web app project?

Asked 2011-04-20T19:45:15.277
13

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.

Edit
Report

1 Answer

2

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

answered 2011-04-20T19:53:35.613

Your Answer