phoenixg 发表于 2012-4-1 13:45:31

index.php的$assign_to_config配置了有什么作用?

问题如标题

kissgxd 发表于 2012-4-1 15:39:30

index.php中没有看见,请问您是在哪里看见的

phoenixg 发表于 2012-4-1 16:28:40

index.php 被注掉了,CodeIgniter.php里面有判断是否存在$assign_to_config

actionbi 发表于 2012-4-1 18:02:24

index.php中注释掉的$assign_to_config

本帖最后由 actionbi 于 2012-4-1 18:06 编辑

phoenixg 发表于 2012-4-1 16:28 static/image/common/back.gif
index.php 被注掉了,CodeIgniter.php里面有判断是否存在$assign_to_configps:回复帖子的时候忘记了点击回复二楼,二楼说的对,我只是在这个基础上细化下。

自己的理解:允许你给一个应用中的控制器,定制config

发现这行代码:
最初,这个$assign_to_config出现在index.php中的108-124行(不过是被注释掉的)

/*
* -------------------------------------------------------------------
*CUSTOM CONFIG VALUES
* -------------------------------------------------------------------
* The $assign_to_config array below will be passed dynamically to the
* config class when initialized. This allows you to set custom config
* items or override any default config values found in the config.php file.
* This can be handy as it permits you to share one application between
* multiple front controller files, with each file containing different
* config values.
* Un-comment the $assign_to_config array below to use this feature
*/
// $assign_to_config['name_of_config_item'] = 'value of config item';

这段话的意思就是在初始化的时候,$assign_to_config array会动态的向config数组中传递数据。
有了它你就能重写或者定义常用的配置。这个机制很方便,因为它允许你共享一个应用程序之间
多个前端控制器文件,而这几个控制器中各有各的不同配置。

其实说白了,就是你可以给一个应用的不同控制器定制配置

实现的原理:
在CodeIgniter.php 139-143行有这么一段话

// Do we have any manually set config items in the index.php file?
if (isset($assign_to_config))
{
$CFG->_assign_to_config($assign_to_config);
}


这段话的意思就很明显了,如果你定义了$assign_to_config数组。那么,加载并实例化完config类后,$CFG对象会自动将
这个数组的配置加载到config.php中定义的config数组里面。

感悟:
再次体会到了CI架构的强大。






页: [1]
查看完整版本: index.php的$assign_to_config配置了有什么作用?