CodeIgniter Forums
Controller Organisation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Controller Organisation (/thread-29887.html)



Controller Organisation - El Forum - 04-25-2010

[eluser]Marvin_S[/eluser]
Hey Guys,

I am fairly new to this CodeIgniter thingybob. Actually I am just about to start my first real project in CodeIgniter. Anyway, I think I understood the very basics of MVC etc. but there is still one thing where I am sure that I can profit from the experience of the long-time CodeIgniter users: The organisation of my controllers.

My example:

I have to create a comparable simple Admin Backend for a customer. So far I used structured programming techniques without any framework. I had the following structure in mind:

Admin login:
-> ACP Root
--> Article-Management (Show Articles, Create Articles, Delete Articles, Edit Articles)
--> Gallery-Management (Show Galleries, Create Pictures, Delete Pictures, Edit Pictures)

So now I wondered how to organize my controllers. My first thought was creating one controller for each main feature and create a function for each function. (Makes sense, heh?)

So for instance I got following controllers with the following functions in addition to the index function:

C1: login (functions: validate_input)
C2: Root (none)
C3: Article (functions: show, add, drop, edit)
C4: Gallery (functions: show, add, drop, edit)

Now that looked great for me first but after some further thinking I noticed I still miss validation proceses for the certain add/edit functions (and most likeley even more) so I need to create more functions for each controller... This made me think again: For better organisation, would it be better to create additional controllers for show, add, drop ... etc.? I think if I would just pack all functions related to Article into one Controller I made end up in huge Controller files.

Anybody with ideas on this? Which way is better? Maybe a completely different way? I hope I could make my point clear. If there are any questions, just ask.

Thanks in advance,
Marvin

PS: If you wonder why you can't create/drop/delete galleries, it's because each article will receive his own possible gallery automatically. So creating/deleting/editing articles will influence already the galleries.


Controller Organisation - El Forum - 04-25-2010

[eluser]pickupman[/eluser]
I've done it both ways. When I first started using CI, I put everything in the Admin controller for all of my admin stuff. Then as you mentioned it became of lot of nested stuff in the code where the methods almost become the controllers. It became a lot easier to maintain the code later, when you can go back in various controllers and update/edit the files. You may also find it help that you could port your code to another application you may write in the future. Suppose you wrote a blog/news controller to generate front end page content, you may able to drop that controller into another app.
Check out this post in regards to controlling front end and back end controllers.


Controller Organisation - El Forum - 04-25-2010

[eluser]Marvin_S[/eluser]
Ah, awesome feedback. Thank you. That underlined what I have thought. And thx for the link too!