Hide Codeigniter controller name from URL with same routes

冷暖自知 提交于 2019-12-11 02:06:17

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/13350947/hide-codeigniter-controller-name-from-url-with-same-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!