EDIT:
What is the best way to structure complex applications with CodeIgniter? To make this question more specific, maybe to just focus on structuring controllers: If you have a Users controller, than should all functions be in this one file? In other words, you might have controller actions that tie to specific views, but a bunch of helper functions as well.
ORIGINAL QUESTION: Given a complex application, with Users, Transactions, Products, does it make sense to have a Controller for each of them? And since each of these corresponds to a database table, to have a corresponding model for each of them? I think it does, but an application I am currently working on consists of one 3000 line controller and one 3000 line model. Just want to verify the standard practice regarding CI and the application structure.
I’d like to share my application structure here.
I start with model. I write 1 model for one table in the mysql database. I already have
MY_Modelclass that I put insystem/application/libraries/folder. This class haveget_detail,get_list,get_total,get_all,insert,update, anddeletemethod. I store the table name in a var, so basically I just need this code in model to make it worked:Update: after some more project, I have added new var to hold the column name used for primary key in the table. This way, I will have more flexibility by not hard coded the column name for primary key in
MY_Model.For the controller, I create it according to it’s usage by user. Example for a product, I will have this controller:
View is related to controller. For above controller, I will have at least 3 view file:
View can be placed in subdir, for example, I can arrange it like this:
If product have category, I will need another table and model for it, but for controller, I can put the page inside Product controllers, by adding category into the function name:
That’s what I do and it’s work for me. Hope this help you find a better way to refactor your CodeIgniter’s code.