0

我正在尝试根据用户角色动态更改路线。所以我有一个缓存 routes.php ,当用户登录时我会写入它。无论如何,我需要一些 url 不被重新路由:

现在我有: $route['(:any)']='CA/$1';在任何网址上都可以正常工作。

auth我需要所有以或rest保留为开头的 URI,因为第一个是登录控制器,第二个是带有 JQgrids 使用的控制器的子文件夹。我试过这个但没有结果:

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

编辑:@Vlakarados

您的路线目前正在运行,但我有以下问题:

我的routes.php样子是这样的:

$route['404_override'] = '';
$route['default_controller'] = "auth";

include_once(APPPATH."cache/routes.php");

在 Auth 控制器中,我执行以下操作:

class Auth extends CI_Controller {
    function Auth() {
        parent::__construct();
    }
    function index()
    {
        if ($this->ion_auth->logged_in())
        {
            $this->__checkRoles();
        }
        else
        {
                        //render the form
            }
    }

    function logout()
    {
        session_start();
        session_unset();
        session_destroy();
        $logout = $this->ion_auth->logout();
        redirect('auth');
    }
    function __checkRoles()
    {

        $role=$this->ion_auth->get_group();
        $this->save_routes($role);
        switch ($role) {

            case 'CA':
                redirect(base_url()); //this is how I'm redirecting now, because it will route to base_url()/CA (but it won't because first it will route to default_controller which is 'Auth'
                break;
            case 'CD':
                redirect(base_url().'CD'); //this is how I used to redirect,
                break;
            case 'DV':
                redirect(base_url().'DV');
                break;
            case 'AC':
                redirect(base_url().'AC');
                break;

            case 'Admin':
                redirect(base_url().'Admin');
                break;
            default:
                show_404();
                break;
        }
    }
    public function save_routes($controller=null)
    {
            $output="<?php ";
            $output.="\$route['auth']='auth';";     
            $output.="\$route['rest']='rest';";
            $output.="\$route['(:any)']='".$controller."/$1';";

            $this->load->helper('file');
            write_file(APPPATH . "cache/routes.php", $output);
            $string = read_file('./path/to/file.php');
    }
}

解决了:

我通过不重定向到 base_url() 并期望base_url().'/CA'哪个当然导致base_url().'/CA/index'但直接重定向到base_url().'/index'

4

1 回答 1

0

尝试这个:

$route['auth']='auth';
$route['auth/(:any)']='auth/$1';
$route['rest']='rest';
$route['rest/(:any)']='rest/$1';

$route['(:any)']='CA/$1';

首先到达的路线具有最高优先级

于 2012-09-06T13:41:27.690 回答