Reputation: 2821
In my CI application I have a few controllers to handle different user-type functions:
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)
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.
Upvotes: 0
Views: 3151
Reputation: 2821
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");
Upvotes: 1
Reputation: 64690
It sounds like what you want is to implement remapping: the standard routing system is not designed to let you alter the routes based on whether or not the user is logged in.
However, you MIGHT (I've never seen this working) be able to put conditional statements in the standard routes.php file that checks to see if the user is logged in and what their role is (look at a session cookie or something like that) and then load different route options. Never tried it, but that MIGHT work for you.
Upvotes: 0