为什么模型首先加载在Codeigniter中


Why is model loaded in first in Codeigniter?

我有检测网站当前语言的用户库currentLang。还有一个模型可以请求并从中获取数据。

请求基于当前语言。这是在自动加载中设置的库currentLang。问题是,它首先在库之后加载Codeigniter中的所有模型。因此,当我打开网站时,我会得到一个空页面,因为查询是在没有库中参数的情况下完成的。

我必须以这种方式改变什么?

先加载哪一个并不重要,只要你能够在CodeIgniter实例中注入当前的语言id,你就可以了。

示例:

// Library
class CurrentLang  {
    // A variable that will hold the CodeIgniter object.
    protected $CI;
    public function __construct () {
        $this->CI = get_instance();
        $this->set_language();
    }
    protected function set_language () {
        // Some code to determine which language to set (your logic goes in determine_language method in this class)
        $Language = $this->determine_language();
        // then we inject the language id in the instance as follows.
        $this->CI->CurrentLanguageId = $Language->id;
    }
}
// Model
class Some_model extends CI_Model {
    public function get_posts () {
        $q = $this->db
                ->where('language_id', $this->CurrentLanguageId)
                ->limit(10)
                ->get('posts');
        return $q->result();
    }
}

请记住,在CodeIgniter 3x中,库名称中的第一个字母应该大写。