Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I little bit new to the CodeIgniter framework and this is my first project with this framework. During a chat on StackOverflow somebody said that we need to make controllers tiny as possible.

Currently I have a default controller named home with 1332 lines of codes (and increasing) and a model named Profunction with 1356 lines of codes (and increasing).

The controller class have about 46 functions on it and also with model class.

I thought that Codeigniter can handle large Controllers or Models well, is there any problem/performance issue/security issues regarding this?

share|improve this question
add comment

2 Answers

The purpose of the MVC pattern is to achieve separation of domain logic from the user interface, following the principle of Separation of Concerns:

In computer science, separation of concerns (SoC) is the process of separating a computer program into distinct features that overlap in functionality as little as possible. A concern is any piece of interest or focus in a program. Typically, concerns are synonymous with features or behaviors. Progress towards SoC is traditionally achieved through modularity of programming and encapsulation (or "transparency" of operation), with the help of information hiding. Layered designs in information systems are also often based on separation of concerns (e.g., presentation layer, business logic layer, data access layer, database layer).

While this is great and it's beautifully taken care of by CodeIgniter, it's not enough. The same principles apply to the individual components of the MVC triad (Model, View and Controller). Concerns should be as separate as possible, it's up to you to identify what those concerns are for your application, but if you put everything in one controller and one model you are probably doing it wrong.

The MVC pattern dictates that the controller's only job should be:

The controller receives user input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and a view port to perform actions based on that input.

So it shouldn't be that fat. It's job is to accept input, validate it and pass it on to the model and / or the view. As an oversimplification on what the concerns might be:

  • A controller should vaguely represent a page in your website,
  • A model should represent a table in your database.

So a controller should be concerned for one page and a model for one table. If your website has other pages than home, let's say a contact page then you should consider a page controller. That way it will be a lot easier for you to mentally connect parts of your application with the actual code, and greatly help with maintenance. By separating code in smaller logical parts, your code will be more robust and easier to test as per the Single Responsibility principle:

In object-oriented programming, the single responsibility principle states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.

The numbers you have in your question are a little much. I can't safely comment on whether what you are doing could be improved, you should post the actual code for peer-review at Code Review Stack Exchange.

There is a small performance issue as well. Everytime you use a class, the class is loaded fully. The overhead is minuscule, but worth noting.


Further reading:


As suggested by @melee in the comments you should look into CodeIgniter helpers, as a better place to put a lot of commonly used functionality. You can easily create your own helpers and have some of your functions in them and not in your controllers. And you should learn about hooks to easily extend the framework core.

share|improve this answer
1  
I get this as an overview of MVC, but I think some information regarding helpers would benefit the OP more, as functions should probably be defined there? –  Nic Nov 30 '11 at 5:20
    
@melee Well I have to see the code first, that's why I suggested Code Review SE. I don't think it's within the scope of Programmers to discuss implementation details, and helpers although present in most MVC frameworks are not an integral concept of the pattern. I've updated the answer with hints to helpers and hooks. –  Yannis Rizos Nov 30 '11 at 5:30
    
I agree, but I think a lot of people who are new to MVC don't realize the role of helpers, so at 1000+ lines of code, I'm sure they'd come in handy somewhere :) –  Nic Nov 30 '11 at 5:51
    
Thanks you ,i am utilizing almost all Codeigniter Helpers,its not a problem for me,I will post it on Code review as soon as possible ,but i need a revision here,and now on i break the controller. –  Red Nov 30 '11 at 6:00
    
You should also learn how to create your own libraries for CodeIgniter applications to carry out tasks that a bit too involved for just helpers. The more can abstract away some of the work done in controllers for reusable purposes, the better. –  Chris C Nov 30 '11 at 7:31
show 1 more comment

About the "make controllers as tiny as possible", apparently the thin-controller-thick-model was being referred to there. This might help a bit, http://mikebernat.com/blog/MVC_-_Fat_Models_and_Skinny_Controllers_

46 functions in a class: Probably you are making one class do too many (possibly unrelated) things.

It is not about whether codeigniter can or cannot handle large models and controllers. It is about ease of maintenance for programmers. If you have one class doing many unrelated things, it will be difficult to read and understand. And anything that is difficult to understand will be difficult to modify.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.