Codeigniter在静态函数中加载库

时间:2013-03-26 06:54:40

标签: php model codeigniter-2

我的users_model中有一个名为isLoggedin()的静态函数,它需要我创建的库。我无法加载它!它位于名为SessionsHelper.php的libraries文件夹中,并且里面有一个SessionsHelper类(我知道并不奇怪)。我尝试在$this->load->library('SessionsHelper')的静态函数中加载它只是为了发现我不能在静态函数中使用$ this。然后我尝试使用require_once '../libraries/SessionsHelper.php'加载它,这不起作用,然后我尝试了自动加载器,它也不起作用....所以这是函数。

public static function isLoggedin() {
        if (SessionsHelper::get('id') != '' && SessionsHelper::get('id') >= 0) { // TODO add logic to differentiate between user/admin
            return true;
        }
        return false;
    }

我只需要能够使用该库!请任何帮助都非常感激!

编辑: 控制器是:

class Users extends CI_Controller{

    public function login(){
        $this->load->model('users_model');

        if($this->users_model->isLoggedin()){
            redirect($this->config->base_url);
        }

        $data['info'] = $this->users_model->login('slavigne@uark.edu', 'abcdefg');

        $this->load->view('templates/header');
        $this->load->view('users/login', $data);
        $this->load->view('templates/footer');
    }

    public function logout(){
        $this->load->library('SessionHelper');
        SessionsHelper::destroy();
    }
}

模型是:

class users_model extends CI_Model{
    public function users_model(){
            parent::__construct();
    }

    public static function hash($password, $salt) {
//this code works no problem! but it's private you know?
    }

    public static function isLoggedin() {
        $CI =& get_instance();
        $CI->load->library('SessionsHelper');
        if (SessionsHelper::get('id') != '' && SessionsHelper::get('id') >= 0) { // TODO add logic to differentiate between user/admin
            return true;
        }
        return false;
    }

    public function login($email, $password){
        $this->load->library('SessionHelper');
        $this->load->database();
        $query = $this->db->get_where('users', array('email' => $email));
        $result = $query->result();
        if($result[0]->password == $this->hash($password, $result[0]->salt)){
            //error_log("Pass matched!");
            SessionHelper::set('id', $result[0]->id);
        }        
        return $result[0];
    }
}

库是,并且在application / libraries / Sessinshelper.php中:

class Sessionshelper {
        public function __construct() {      
    }

    public static function start() {
        if (!session_id()) {
            session_start();
        }
    }

    public static function set($fld, $val) {
        self::start();
        $_SESSION[$fld] = $val;
    }

    public static function un_set($fld) {
        self::start();
        unset($_SESSION[$fld]);
    }

    public static function destroy() {
        if (session_id() != "" || isset($_COOKIE[session_name()])) {
            $params = session_get_cookie_params();
            setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
        }
        session_unset();
        $_SESSION = array();
        session_destroy();
    }

    public static function get($fld) {
        self::start();
        if (isset($_SESSION[$fld])) {
            return $_SESSION[$fld];
        }
        return FALSE;
    }
}

1 个答案:

答案 0 :(得分:1)

您可以通过使用get_instance加载全局可访问的 CodeIgniter实例,在静态上下文中加载任何库,从而产生:

$CI =& get_instance();
$CI->load->library('SessionHelper');

但是静态方法通常被社区认为难以测试和设计气味,尤其是在使用像CodeIgniter这样的框架时可以避免它。