用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3208|回复: 3
收起左侧

[已解决] 自动加载同名的配置文件

[复制链接]
发表于 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中,相关代码如下:
PHP复制代码
 
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,
PHP复制代码
if ($config !== NULL)
                {
                        $CI->$classvar = new $name($config);
                }
                else
                {
                        $CI->$classvar = new $name;
                }
复制代码

无法进行有参构造,那我如何能才能自动加载同名配置文件并在自定义的library中使用配置文件中的变量呢?
谢谢
发表于 2011-3-1 15:48:12 | 显示全部楼层
本帖最后由 jeongee 于 2011-3-1 15:50 编辑

哦?你在你的library里加载不好嘛,那样的话系统加载你的library不就加载了配置了么,怎么样按照CI的方式用配置文件,请参考http://codeigniter.org.cn/user_guide/libraries/config.html
 楼主| 发表于 2011-3-1 18:12:23 | 显示全部楼层
是啊,非常感谢。我就琢磨难道有什么特别的方式去赋。。。
发表于 2011-3-1 23:08:41 | 显示全部楼层
include 后就会修改 $config 变量,你看看 config 文件的格式就知道了。

本版积分规则