Welcome Guest, Not a member yet? Register   Sign In
Sub-Controllers?
#1

[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!
#2

[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
  function favorites($method) {
    $args = func_get_args();

    if (method_exists($this, $method)
    {
      call_user_func_array(array($this,'_favorites_'. $method),array_slice($args,1));
    }
    else
    {
      show_404();
    }
  }
#3

[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 Smile Thanks again!
#4

[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.




Theme © iAndrew 2016 - Forum software by © MyBB