Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 721
Zeeshan Avatar asked Apr 20 '11 19:04

Zeeshan


People also ask

What is controller in MVC CodeIgniter?

It can also be an RSS page, or any other type of “page”. The Controller serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.

Does CodeIgniter use MVC?

Like most of the web frameworks, CodeIgniter uses the Model, View, Controller (MVC) pattern to organize the files. This keeps the data, the presentation, and flow through the application as separate parts.

What is MVC draw and explain MVC architecture for developing web applications?

The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. Each of these components are built to handle specific development aspects of an application.

Why do we use MVC in PHP?

MVC allows you to separate your business logic from your presentation layer. This "Separation of Concerns" allows you to quickly find and edit portions of your code. It also enables easy reuse of your UI components across your system.


2 Answers

(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.

like image 193
Hubro Avatar answered Oct 02 '22 14:10

Hubro


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

like image 45
jondavidjohn Avatar answered Oct 02 '22 15:10

jondavidjohn