codeigniter:从其他配置文件访问配置变量?

发布于 2024-10-06 22:01:56 字数 487 浏览 0 评论 0原文

我有两个配置文件。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

独闯女儿国 2024-10-13 22:01:56

我检查过,在请求生命周期中,CodeIgniter 对象的 config 属性只是一个数组,而不是配置对象。

因此,您应该能够像这样获取配置设置:

$this->config['env']

所以这应该可行:

if ($this->config['env'] == 'hailwood_dev')
{
    //email variables like smtp server to do with localhost
} 
elseif ($this->config['env'] == 'production'){
    //email variables like smtp server to do with production
}

如果您要自动加载配置文件,请确保它们以正确的顺序自动加载。配置文件必须在它依赖的任何文件之后自动加载。

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 :

$this->config['env']

So this should work :

if ($this->config['env'] == 'hailwood_dev')
{
    //email variables like smtp server to do with localhost
} 
elseif ($this->config['env'] == 'production'){
    //email variables like smtp server to do with production
}

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.

时间海 2024-10-13 22:01:56

将其添加到您的电子邮件配置文件中。

$environment = config_item('env');
OR THIS
$environment = $this->config->item('env');

不确定第一个是否适用于您的设置。大多数人似乎使用第二个。

Add this to your email config file.

$environment = config_item('env');
OR THIS
$environment = $this->config->item('env');

Not sure if the first will work on your setup. Most people seem to use the second.

不弃不离 2024-10-13 22:01:56

试试这个:

 $env = $this->config->item('env');

 if ($env == "dev_server")  {
   // Do this...
 }
 else  {
   // Do this..
 }

Try this:

 $env = $this->config->item('env');

 if ($env == "dev_server")  {
   // Do this...
 }
 else  {
   // Do this..
 }
软糯酥胸 2024-10-13 22:01:56

在这种情况下,您需要将配置放在

  • application/config/ceveloppment/my_config.php
  • application/config/products/my_config.php

http://codeigniter.com/user_guide/libraries/config.html

与其他awnsers结合,您可以在同一环境的两个配置文件之间共享信息

In this case you would want to put configuration in

  • application/config/ceveloppment/my_config.php
  • application/config/production/my_config.php

http://codeigniter.com/user_guide/libraries/config.html

Combining with other awnsers you could share information between two configuration files for the same environnement

花海 2024-10-13 22:01:56

使用 config_item 函数 (system/core/Common.php)

/**
* 返回指定的配置项
*
* @访问公共
* @return 混合

use config_item function (system/core/Common.php)

/**
* Returns the specified config item
*
* @access public
* @return mixed

倾城月光淡如水﹏ 2024-10-13 22:01:56

请参阅下面的解决方案

$this->config->config['base_url']

echo "<pre>";
print_r($this->config);

上面将打印所需的详细信息

Please see below solutions

$this->config->config['base_url']

echo "<pre>";
print_r($this->config);

Above will print required details

逆蝶 2024-10-13 22:01:56

在 CodeIgniter 3.0 中,访问另一个配置文件中的配置变量的解决方案是获取 CodeIgniter 实例。一旦获得了对它的引用,您就可以使用 item 函数进行访问。

$CI =& get_instance();
$var = $CI->config->item('item_name');

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.

$CI =& get_instance();
$var = $CI->config->item('item_name');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文