![]() |
Sub-Controllers? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Sub-Controllers? (/showthread.php?tid=10852) |
Sub-Controllers? - El Forum - 08-14-2008 [eluser]cyang[/eluser] Hey all, controllers question, aong with my current attempt at a solution. Not sure the exact term, but is there such a thing as a "sub-controller"? For example, I have a "user" controller which I'm placing all user related stuff functions, like "login" and "register". The issue is that I'd also like an additional layer/hierarchy of routing. Another category of user-related actions has to do with maintaining favorites lists (e.g. of other users, products, etc), and I'd like URIs in the form of "user/favorites/<action>", e.g. "user/favorites/add" and "user/favorites/remove". I know I can do some custom routing, or maybe perhaps use _remap(), but a way that appeals more to my sense of logic is if I could somehow have a "favorites" controller within the "user" controller. That way, everything would stay self-contained within the controllers and I (and more importantly, others on this project) wouldn't have to update routes.php whenever there are new functions. Is there a CodeIgniter way or best-practice to accomplish this? The way I've been kind of mimicking this functionality is prefixing private functions (e.g. _favorites_add(), _favorites_remove()), and then using reflection and calling them with call_user_func_array() and func_get_args(). For example: Code: class User extends Controller { Anyways, I'd love to hear your guys' thoughts on this whole topic in general. Thanks! Sub-Controllers? - El Forum - 08-15-2008 [eluser]Colin Williams[/eluser] Quote:That way, everything would stay self-contained within the controllers and I (and more importantly, others on this project) wouldn’t have to update routes.php whenever there are new functions. Is there a CodeIgniter way or best-practice to accomplish this? Well, with your current implementation, you've moved the task of updating easy declarations in config/routes.php to implementing several lines of code in your favorites() method. A couple solutions: 1. Just route 'user/favorites/*' to 'favorites/*'. Easy-peasy. 2. Route 'user/favorites/*' to 'user/favorites_*'. Again, pretty straight forward. 3. Do what you're doing now but streamline the method calling: Code: // Mimicking a 'Favorites' controller Sub-Controllers? - El Forum - 08-15-2008 [eluser]cyang[/eluser] Very cool beans to you sir, for the idea to regex re-route to an entire controller. Bonus points for also improving my hokey solution ![]() Sub-Controllers? - El Forum - 08-15-2008 [eluser]Colin Williams[/eluser] I also forgot to mention option 4. Move the favorites controller to a users/ subfolder. application/controllers/users.php application/controllers/users/favorites.php That involves no routing at all. |