Php – Controllers in CodeIgniter

codeignitermvcPHP

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?

Best Answer

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.

Related Topic