Reputation: 11
I have a class in hook folder like this
class CheckAuth {
protected $CI;
public function __construct()
{
$this->CI = get_instance();
}
public function check()
{
$router =& load_class('Router', 'core');
// $controller = $this->CI->router->class;
$controller = $router->fetch_class();
$method = $router->fetch_method();
if($controller!='auth')
{
echo $this->CI->userdata('admin_id');
}
}
}
I show error when I get a session
Fatal error: Call to a member function userdata() on a non-object $this->CI return null.
Upvotes: 1
Views: 2760
Reputation: 497
That's because CI isn't loaded yet in some hooks points (pre_controller, pre_system). You are probably trying to load class in some of these.
Upvotes: 1
Reputation: 986
Try this it will work.
public function __construct()
{
}
public function check()
{
$this->CI = get_instance();
$router =& load_class('Router', 'core');
// $controller = $this->CI->router->class;
$controller = $router->fetch_class();
$method = $router->fetch_method();
if($controller!='auth')
{
echo $this->CI->userdata('admin_id');
}
}
Upvotes: 1