2

i'm just getting started at codeigniter, i want to hide controller name from URL with same routes setup.

i have 3 controllers which are students, staff, teachers having same function called home, this won't work obviously

$route['home'] = 'students/home';
$route['home'] = 'staff/home';

is there any way to accomplish this? i have session data using codeigniter session class containing user type so i tried something like this

session_start()    
$route['home'] = $_SESSION['user_type'].'/home';

but i cant get the session data, maybe its using codeigniter session class?? so, how can i get the data? or is there other solution?

  • What are your URLs? From what I can see, you are trying to route `/home` to multiple controllers/methods, which is not possible. (Unless you have a method that does the routing logic itself.) – Maxime Morin Nov 12 '12 at 20:29
  • localhost/ci_project/students/home, ../staff/home, well i handle each user type with a controller, and each of them have home function. can i do it dynamically? – Budianto Tan Nov 12 '12 at 20:33
  • There's probably a way to do it dynamically, but I'm not sure if CodeIgniter works well with it. For example, you could [load the class](http://stackoverflow.com/questions/5072352/instantiating-class-by-string-using-php-5-3-namespaces) and [call a method](http://stackoverflow.com/questions/1005857/how-to-call-php-function-from-string-stored-in-a-variable) on it based on a string. – Maxime Morin Nov 12 '12 at 21:15
  • okay, gonna try that out.. thanks for the solution – Budianto Tan Nov 13 '12 at 05:44

1 Answers1

2

Perhaps you should write a common controller and disperse by your second URI parameter:

home/students or home/staff

$route['home/:any'] = "home";

and home controller's index method:

public function index()
{
    $type = $this->uri->segment(2);
    switch($type){
        case "student":
            $this->student();
        break;
        case "staff":
            $this->staff();
        break;
        default:
            $this->some_other_method();
        break;
    }
}

Obviously you would create a student and staff method and handle things differently if need be.

A side note - why do you want to conceal the controller's name? It's not like that's a security hole or anything.

Kai Qing
  • 18,793
  • 5
  • 39
  • 57
  • thanks, i get the idea.. well, i have a bout 6 methods in a controller, and i have 4 controller.. oh, i have more work to do.. umm, i have been requested to hide the user type – Budianto Tan Nov 12 '12 at 20:42