0

我想CI_Lang通过创建一个文件来扩展默认 CodeIgniter 的类/core/MY_Lang.php,并且在该文件中我需要加载一个自定义配置文件:

class MY_Lang extends CI_Lang {

    var $CI;

    // Default langs
    var $languages = array(
        'en' => 'english',
        'fr' => 'french'
    );

    public function __construct(){
        parent::__construct();

        global $CFG;

        $this->CI  =& get_instance(); // NOT WORKING

        // Load custom config file from /config/languages.php
        $this->languages = $this->CI->config->item('languages');

    }

}

/config/languages.php自定义配置文件:

$config['languages'] = array(
    'en' => 'english',
    'fr' => 'french',
    'ru' => 'russian',
    'sk' => 'slovak'
);

每当我要求get_instance- 我收到一个错误告诉我Fatal error: Class 'CI_Controller' not found in D:\wamp\www\ci2exp6\system\core\CodeIgniter.php on line 233

拜托,有人可以澄清我的代码有什么问题吗?

4

1 回答 1

4

在核心库中,您需要按如下方式调用它:

$CFG =& load_class('Config', 'core');
$CFG->load('languages', true);

$languages = $CFG->config['languages'];
$english   = $languages['en'];
//etc...

这应该够了吧 :)

从评论更新配置文件:

$config['en'] = 'english';
$config['fr'] = 'french';
$config['ru'] = 'russian';
$config['sk'] = 'slovak';
于 2013-09-06T06:49:37.843 回答