簡體   English   中英

Codeigniter在靜態函數中加載庫

[英]Codeigniter loading a library in a static function

我的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;
    }
}

您可以通過使用get_instance加載全局可訪問的 CodeIgniter實例來加載靜態上下文中的任何庫,從而導致:

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

但靜態方法通常被社區認為難以測試和設計氣味,特別是在使用像CodeIgniter這樣的框架時可以避免它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM