[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 {
// Mimicking a 'Favorites' controller
function favorites($method) {
$args = func_get_args();
switch($method) {
case 'add':
call_user_func_array(array($this,'_favorites_add'),array_slice($args,1));
break;
case 'remove':
call_user_func_array(array($this,'_favorites_remove'),array_slice($args,1));
break;
default:
show_404();
break;
}
}
function _favorites_add($user_id) {
...
}
}
Anyways, I'd love to hear your guys' thoughts on this whole topic in general. Thanks!