在我的CI应用程序中,我有几个控制器来处理不同的用户类型功能:
- CA
- CD
- DV
- CSC
- CB
因此,目前当有人登录时,他会根据自己的角色重定向到(例如):localhost/CA
或localhost/CD
等。
我需要重写路线,根据他的角色重定向所有内容:
$route['(:any)'] = 'CA/$1';
(CA不应硬编码)
- 使用登录控制器时也应该删除该规则(通过过滤一些url)
有人能告诉我登录后如何勾选规则吗?以及如何使用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 ";
//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);
}
我的routes.php看起来像这样:
$route['404_override'] = '';
$route['default_controller'] = "auth";
include_once(APPPATH."cache/routes.php");
听起来你想要的是实现重新映射:标准路由系统的设计并不是让你根据用户是否登录来更改路由。
然而,您可能(我从未见过这种方法)能够将条件语句放在标准routes.php文件中,该文件检查用户是否登录以及他们的角色是什么(查看会话cookie或类似的东西),然后加载不同的路由选项。从未尝试过,但这可能对你有用。