Welcome Guest, Not a member yet? Register   Sign In
find out target controller in autoloaded library
#1

[eluser]Random dude[/eluser]
I've got a library that's being auto-loaded (autoload.php) that uses Ben Edmonds Ion Auth and checks if the user is logged in, and if not redirects them to the login page. Only problem is this causes an infinite loop because this library gets auto-loaded for the login page as well.

How can I find out the what controller is being loaded when I'm in the library that is auto-loaded - for example, in the auto-loaded library I could check to see if the target controller is the login page and if it is, don't do the redirect.

Thanks in advance,
Nicholas.
#2

[eluser]danmontgomery[/eluser]
Generally speaking, I would put all redirect logic in the controller. But you can access the CI object with get_instance(), and check the class name with get_class().

Code:
$CI = get_instance();
$class_name = get_class($CI);

Rather than checking the class name, I would set a class variable to check (again, keeping the redirect logic in the controller):

Code:
class MY_Controller extends CI_Controller {

    protected $_require_login = FALSE;

    public function __construct() {
        parent::__construct();

        if($this->_require_login == TRUE && !$this->some_auth->logged_in()) {
            redirect('login');
            exit();
        }
    }

}

// ...

class Example_controller extends MY_Controller {

    protected $_require_login = TRUE;

}

// ...

class Login_controller extends MY_Controller {

    protected $_require_login = FALSE;

}
#3

[eluser]Random dude[/eluser]
Thank you Noctrum, that's a much better way to do it!
Just a note, it is etiquette to say thank you in the thread when someone answers your questions - or is it just wasting peoples time by bumping up the thread?
#4

[eluser]danmontgomery[/eluser]
No harm in saying thanks Wink




Theme © iAndrew 2016 - Forum software by © MyBB