I always use the first version, it’s probably because when I used procedural code I had different files for each action, and it just stuck. I can see the benefits of having all the edit functions in one controller, for example.
Although it would get messy if you had a modular structure like I use now, which is why I stick with the first. But then again I would have the whole function in a model and just run it from the controller.
The first way makes you write code that is spread out more which makes it easier to eyes.
The second way you can look for code that is similar in the different actions and you can make it DRY.
I use the second way and put the code in the cases up to ten lines per case, if it’s more i use methods so it’s a third way
function user($action) { switch($action) { case 'add': break; case 'edit': $this->_edit(); break; case 'delete': break; } }
I would use A as well. It seems to me to be easier to maintain specific functions than have larger switched functions. Besides, the whole point of object oriented programming is to group functions and variables into data types. Effectively giving you the badass version of method B.
You get the idea. Using your imagination a little, CI becomes one heck of a search engine optimization tool since it allows you to craft how your urls will appear.
just a note: I use add and edit often in the same function call (often it is also the same view with the same form). If an id has been transfered, I would need to mutate a DB-record, otherwise I do an insert.
from my point of view, if you think about CI and MVC style, the controller class is the “topic” (who), the controller method is the “action” (what), and the other segments are the arguments.
That’s why I’ll go for option A.
@dready This works perfectly for relatively small controllers (<100 functions) but for larger controllers I find method B to be preffered. Of course, this is just how I like to work.
One can also split controllers but then the theory/practice of topic-method-arguments is busted.
So for small controllers I go for method A, Large controllers I go for method B, extremely large controllers I go for method C