Codeigniter动态路由 [英] Codeigniter dynamic routes

查看:95
本文介绍了Codeigniter动态路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的CI应用程式中,我有几个控制器来处理不同的使用者类型功能:


  1. CA

  2. CD

  3. DV

  4. CSC

  5. CB

所以目前有人登录时,他被他的角色重定向到(example): localhost / CA code> localhost / CD 等。



我需要重写路由以根据他的角色重定向一切:



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


  • 使用登录控制器(通过过滤某些网址)也应删除该规则



任何人都可以告诉我如何在登录后挂断规则?以及如何使用regexp过滤一些url应用规则?

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

还有什么其他方法可以实现呢? .htaccess是没有问题,因为我需要一些数据逻辑来创建路由。

解决方案

感谢关于从数据库路由,我管理教程在用户登录后(在我的Auth控制器)添加新路由:

  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;
//一些uri的不需要路由
$ output。=\ $ route ['auth'] ='auth';;
$ output。=\ $ route ['auth /(:any)'] ='auth / $ 1';;
$ output。=\ $ route ['rest'] ='rest';;
$ output。=\ $ route ['rest /(:any)'] ='rest / $ 1';;

//通过用户类型控制器的剩余路由
$ output。=\ $ route ['(:any)'] ='。 / $ 1';;

$ this-> load-> helper('file');
//写入缓存文件
write_file(APPPATH。cache / routes.php,$ output);
}

我的routes.php如下所示:

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

include_once(APPPATH。cache / routes.php);


In my CI application I have a few controllers to handle different user-type functions:

  1. CA
  2. CD
  3. DV
  4. CSC
  5. CB

So currently when someone logs in, he is redirected by his role to (example) : localhost/CA or localhost/CD etc..

I need to rewrite the routes to redirect everything depending on his role:

$route['(:any)'] = 'CA/$1'; (CA should not be hardcoded)

  • The rule should also be removed when using the login Controller (by filtering some url's)

Can anyone show me how to hook the rules after login? and also how to use a regexp to filter some url on which to apply the rules?

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

What other way would be to achieve this? .htaccess is out of the question since I need some data logic to create the routes.

解决方案

Thanks to this wonderfull tutorial about routing from the database, I managed to add new routes after the user logs in (in my Auth controller):

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);
    }

My routes.php looks like this:

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

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

这篇关于Codeigniter动态路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆