如何在CodeIgniter的“core”文件夹中加载自定义配置文件

时间:2013-09-06 06:30:18

标签: php codeigniter

我想通过创建文件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

请有人澄清我的代码中有什么问题吗?

1 个答案:

答案 0 :(得分:3)

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

$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';