codeigniter:从其他配置文件访问配置变量?
我有两个配置文件。
config.php(代码点燃核心配置)
和email.php(使用电子邮件类时自动加载)
我想要做的是。
在 config.php 中有
$config['env'] = 'hailwood_dev';
然后在 email.php 中有
if($config['env'] == 'hailwood_dev'){
//email variables like smtp server to do with localhost
} elseif($config['env'] == 'production'){
//email variables like smtp server to do with production
}
但这没有任何效果(我猜测 $config['env'] 没有具有这些值)。
我怎样才能访问这个值?
I have two config files.
config.php (code igniters core config)
and email.php (autoloaded by the email class when it is used)
What i am wanting to do is.
In config.php have
$config['env'] = 'hailwood_dev';
then in email.php have
if($config['env'] == 'hailwood_dev'){
//email variables like smtp server to do with localhost
} elseif($config['env'] == 'production'){
//email variables like smtp server to do with production
}
But this is not having any effect (im guessing as $config['env'] does not have those values).
How can I access this value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我检查过,在请求生命周期中,CodeIgniter 对象的 config 属性只是一个数组,而不是配置对象。
因此,您应该能够像这样获取配置设置:
所以这应该可行:
如果您要自动加载配置文件,请确保它们以正确的顺序自动加载。配置文件必须在它依赖的任何文件之后自动加载。
I checked and at that point in the request lifecycle the config property of the CodeIgniter object is just an array, not a config object.
So you should be able to get at your configuration setting like this :
So this should work :
If you are autoloading configuration files, make sure they are autoloaded in the correct order. A configuration file must be autoloaded after any it depends on.
将其添加到您的电子邮件配置文件中。
不确定第一个是否适用于您的设置。大多数人似乎使用第二个。
Add this to your email config file.
Not sure if the first will work on your setup. Most people seem to use the second.
试试这个:
Try this:
在这种情况下,您需要将配置放在
http://codeigniter.com/user_guide/libraries/config.html
与其他awnsers结合,您可以在同一环境的两个配置文件之间共享信息
In this case you would want to put configuration in
http://codeigniter.com/user_guide/libraries/config.html
Combining with other awnsers you could share information between two configuration files for the same environnement
使用 config_item 函数 (system/core/Common.php)
/**
* 返回指定的配置项
*
* @访问公共
* @return 混合
use config_item function (system/core/Common.php)
/**
* Returns the specified config item
*
* @access public
* @return mixed
请参阅下面的解决方案
$this->config->config['base_url']
上面将打印所需的详细信息
Please see below solutions
$this->config->config['base_url']
Above will print required details
在 CodeIgniter 3.0 中,访问另一个配置文件中的配置变量的解决方案是获取 CodeIgniter 实例。一旦获得了对它的引用,您就可以使用
item
函数进行访问。In CodeIgniter 3.0 the solution to accessing config variables in another config file is to get the CodeIgniter instance. Once you have a reference to it you can then access using the
item
function.