Welcome Guest, Not a member yet? Register   Sign In
Calling Controllers
#1

[eluser]Unknown[/eluser]
Im trying to get a base done for a permission based admin system
i have to add alot of pages foreach type of admin,
so naturally I would want to split that code

all i would want to do is if the session isset get the permission (string)
from that current logged in user and use it as a way to access another
controller (class/method), therefor splitting all those different admin pages into their own

//part of the code
Code:
//in main controler Admin
function __construct() {
    parent::__construct();
    if ($this->session->userdata('admin_logged_in')) {
        $admin_type = $this->session->userdata('admin_type');
        
        //send it to ohter controller here
        
        //something of the like:
        include APPPATH.'/controllers/site_admin/site_admin_'.$admin_type.'.php';
        // OR/AND:
        $admin_type->$method
    }
}

public function login()
public function logout()
etc



//the 3 different admin types (in their classes here)
class workers        extends CI_Controller { /* their pages/functions/methods here */ }
class coordinators   extends CI_Controller { /* their pages/functions/methods here */ }
class administration extends CI_Controller { /* their pages/functions/methods here */ }


so i could call

site.we/admin/somefunc

and either be directed to

workers/somefunc
or
coordinators/somefunc

depending who logged in with what permission(s)



i would have wanted to use the router
but i cant access the session there
..so, is there anyway to send the same request to a different controller, from a controller ???


also, im not worried about duplication in cases, like having the same method/page in different controllers
like /admin/workers/somefunc and /admin/coordinators/somefunc
aslong as i can keep the administrators separate in an easy way




thanx in advanced
great forum =D
#2

[eluser]Unknown[/eluser]
Oke after lots of tinkering ive gotten this to work:

Code:
//in main controler (Admin) extending CI_Controller
function __construct() {
    parent::__construct();
    if ($this->session->userdata('admin_logged_in')) {
        $admin_type = $this->session->userdata('admin_type');
        
        //something of the like:
        include APPPATH.'/controllers/site_admin/site_admin_'.$admin_type.'.php';
        $class = 'site_admin_'.$admin_type;
        $this->call_admin_class = new $class($this);
    }
}

function _remap($method) {
    switch ($method) {
        case 'login':$this->login();break;
        case 'logout':$this->logout();break;
        default:
        if (method_exists($this->call_admin_class, $method)) {
            call_user_func_array(array(&$this->call_admin_class, $method), array_slice($this->uri->rsegments, 2));
        } else {
            $this->index();
        }
    }
}

public function login()
public function logout()
public function index()
etc, consider theese global overwrites
if you were to call /logout it would this class logout
but calling something like /settings would use the below classes for whatever type of admin is currently logged in (using session)



//the 3 different admin types (in their classes here)
class site_admin_workers        { /* their pages/functions/methods here */ }
class site_admin_coordinators   { /* their pages/functions/methods here */ }
class site_admin_administration { /* their pages/functions/methods here */ }

//then lastly the new class will have to reinit the CI variables under the $this
//(constructor for the 3 classes above)
function __construct($CI) {//we passed $this into here
    foreach ($CI as $key => $value) {
        $this->$key = $value;
    }
}


witch in the end allowed me to use a different class for whatever the user is currently logged into
are there other ways of achieving this
perhaps if i had Better knowledge of &references; etc




Theme © iAndrew 2016 - Forum software by © MyBB