Hello I have created a library Auth.php
in application/libraries
to authenticate if the current user is logged in or not. So my controller is :
$session_data = $this->session->userdata('logged_in');
$this->load->library('Auth');
$auth = new Auth;
$user_id = $auth->authenticate($session_data);
And the library :
class Auth{
function authenticate($vars){
$CI =&get_instance();
$CI->load->model('adminmodels/login_model');
$username = $vars['username'];
$password = $vars['password'];
$user_id = $vars['user_id'];;
$user_type = $vars['user_type'];
$check_login = $this->login_model->login($username,$password); //Line 14
if($check_login){
$user_id = $user_id;
}else{
$user_id = 0;
}
return $user_id;
}
}
But it is showing error like :
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Auth::$login_model
Filename: libraries/Auth.php
Line Number: 14
Whats wrong I am doing ??
While correct generally, in CI you should call the library like this, not with the new
keyword:
$this->load->library('Auth');
$user_id = $this->auth->authenticate($session_data);
Also, since you assigned the global CI object to a variable, you can't use $this
to refer to it:
$check_login = $this->login_model->login($username,$password); //Line 14
should be:
$check_login = $CI->login_model->login($username,$password); //Line 14
(since you loaded the model there: $CI->load->model('adminmodels/login_model');
)
Inside Codeigniter library file, you cannot load any model, config, or any external library using $this by pass without CI instance object/variable
For your case in Auth.php at line 14, you are wrong :
$check_login = $this->login_model->login($username,$password); //Line 14
It should be like this (if access from internal function only) :
$check_login = $CI->login_model->login($username,$password); //Line 14
Or, if access from any functions in the Auth class :
$check_login = $this->CI->login_model->login($username,$password); //Line 14
But first, you must make CI intance object in your construct function like this :
$this->CI =&get_instance();
$this->CI->load->model('adminmodels/login_model');
before you action in second alternative above.
May help you... Thanks