echocsg 发表于 2011-3-1 15:36:40

自动加载同名的配置文件

本帖最后由 echocsg 于 2011-3-1 18:23 编辑

当在控制器里面load->library('abc')时,将会试图加载application/config中的abc.php。
在core/Loader.php类里面,funtion _ci_init_class中,相关代码如下:

if ($config === NULL){
        // Fetch the config paths containing any package paths
        $config_component = $this->_ci_get_component('config');

        if (is_array($config_component->_config_paths)){
                // Break on the first found file, thus package files
                // are not overridden by default paths
                foreach ($config_component->_config_paths as $path){
                        // We test for both uppercase and lowercase, for servers that
                        // are case-sensitive with regard to file names
                        if (file_exists($path .'config/'.strtolower($class).EXT)){
                                include_once($path .'config/'.strtolower($class).EXT);
                                break;
                        }elseif (file_exists($path .'config/'.ucfirst(strtolower($class)).EXT)){
                                include_once($path .'config/'.ucfirst(strtolower($class)).EXT);
                                break;
                        }
                }
        }
}
请教各位,虽然这里include了我的配置文件,却没有改变变量$config的值,最终创建对象的时候,还是会由于$config === NULL,
if ($config !== NULL)
                {
                        $CI->$classvar = new $name($config);
                }
                else
                {
                        $CI->$classvar = new $name;
                }
无法进行有参构造,那我如何能才能自动加载同名配置文件并在自定义的library中使用配置文件中的变量呢?
谢谢

jeongee 发表于 2011-3-1 15:48:12

本帖最后由 jeongee 于 2011-3-1 15:50 编辑

哦?你在你的library里加载不好嘛,那样的话系统加载你的library不就加载了配置了么,怎么样按照CI的方式用配置文件,请参考http://codeigniter.org.cn/user_guide/libraries/config.html

echocsg 发表于 2011-3-1 18:12:23

:lol是啊,非常感谢。我就琢磨难道有什么特别的方式去赋。。。

Hex 发表于 2011-3-1 23:08:41

include 后就会修改 $config 变量,你看看 config 文件的格式就知道了。
页: [1]
查看完整版本: 自动加载同名的配置文件