使用相同的路由从URL隐藏Codeigniter控制器名称

使用相同的路由从URL隐藏Codeigniter控制器名称

问题描述:

我刚开始使用codeigniter,我想使用相同的路由设置从URL中隐藏控制器名称。

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

我有3个控制器,分别是学生,工作人员,具有相同功能的教师称为家,这显然不会起作用

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';

有什么方法可以做到这一点?我有使用包含用户类型的codeigniter会话类的会话数据,所以我尝试了类似的操作

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';

但是我无法获取会话数据,也许它使用了codeigniter会话类?因此,我如何获取数据?还是有其他解决方案?

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?

也许您应该编写一个通用控制器并通过第二个URI参数进行分散:

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.