Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter动态路由_Php_Codeigniter - Fatal编程技术网

Php Codeigniter动态路由

Php Codeigniter动态路由,php,codeigniter,Php,Codeigniter,在我的CI应用程序中,我有几个控制器来处理不同的用户类型函数: CA 光盘 DV CSC CB 因此,当前当有人登录时,他的角色会将他重定向到(例如):localhost/CA或localhost/CD等 我需要根据他的角色重写路由以重定向所有内容: $route['(:any)]='CA/$1'(CA不应硬编码) 使用登录控制器时也应删除该规则(通过过滤某些url) 有人能告诉我如何在登录后钩住规则吗?以及如何使用regexp来过滤一些要应用规则的url $route['^((?!auth

在我的CI应用程序中,我有几个控制器来处理不同的用户类型函数:

  • CA
  • 光盘
  • DV
  • CSC
  • CB
  • 因此,当前当有人登录时,他的角色会将他重定向到(例如):
    localhost/CA
    localhost/CD

    我需要根据他的角色重写路由以重定向所有内容:

    $route['(:any)]='CA/$1'(CA不应硬编码)

    • 使用登录控制器时也应删除该规则(通过过滤某些url)
    有人能告诉我如何在登录后钩住规则吗?以及如何使用regexp来过滤一些要应用规则的url

    $route['^((?!auth/).)*$'] = '$1';
    

    还有什么其他方法可以实现这一点。htaccess是不可能的,因为我需要一些数据逻辑来创建路由

    听起来您想要实现的是:标准路由系统的设计不允许您根据用户是否登录来更改路由

    但是,您可能(我从来没有见过这样做)能够在标准routes.php文件中放入条件语句,检查用户是否登录以及他们的角色是什么(查看会话cookie或类似内容),然后加载不同的路由选项。从未尝试过,但这可能对您有用。

    多亏了有关数据库路由的精彩教程,我在用户登录后(在我的身份验证控制器中)成功添加了新路由:


    仅供参考,我正在使用ion_auth,你能告诉我更多细节吗?不幸的是,我对ion_auth不太熟悉:无论如何,你可能需要重新映射。CI文档很好地涵盖了这一点。我不能在控制器中使用_remap函数,因为当该函数被命中时,控制器理论上已经知道了。我需要“上一级”
    class Auth extends CI_Controller {
    function Auth() {
        parent::__construct();
    }
    function index()
    {
        if ($this->ion_auth->logged_in())
        {
            $this->__checkRoles();
        }
    function __checkRoles()
        {       
            $role=$this->ion_auth->get_group();
            $this->save_routes($role);
    
            redirect(base_url().'index');
    
        }
        public function save_routes($controller=null)
        {
                $output="<?php ";
                        //some uri's don't need routing
                $output.="\$route['auth']='auth';";         
                $output.="\$route['auth/(:any)']='auth/$1';";                       
                $output.="\$route['rest']='rest';";
                $output.="\$route['rest/(:any)']='rest/$1';";
    
                        //for the rest route trough the "user-type" controller
                $output.="\$route['(:any)']='".$controller."/$1';";
    
                $this->load->helper('file');
                        //write to the cache file
                write_file(APPPATH . "cache/routes.php", $output);
        }
    
    $route['404_override'] = '';
    $route['default_controller'] = "auth";
    
    include_once(APPPATH."cache/routes.php");