为什么我的配置项不会从codeigniter中的getenv()条目填充?

时间:2016-12-11 16:41:31

标签: php codeigniter environment-variables

我正在使用phpdotenv和Codeigniter。 Codeigniter的环境设置对这个项目来说并不适用。

我试图在我的config.php文件中设置它:

$config['site_id'] = getenv('APP_ID');

phpdotenv正在通过pre_system挂钩加载,getenv(' APP_ID')在整个应用程序中都可用。我还检查了核心,并在加载配置项之前将其激活。

$hook['pre_system'] = function() {
 $dotenv = new Dotenv\Dotenv(APPPATH);
 $dotenv->load();
};

$this-config->item('site_id')的值始终为NULL

对于为什么会发生这种情况的任何建议都会非常感激。

提前致谢。

2 个答案:

答案 0 :(得分:0)

哦,您基本上只需要这样做:

require APPPATH . 'vendor/autoload.php';
  $dotenv = new Dotenv\Dotenv(BASEPATH . '../');
  $dotenv->load();

答案 1 :(得分:0)

如@ jhodgson4所述,CodeIgniter首先在框架(core/CodeIgniter.php)的引导期间加载配置的常量。在加载CodeIgniter.php之前,您可以在index.php文件的底部要求dotenv并将其初始化。这应该允许您在任何配置文件中使用getenv。

// ...at the bottom of index.php
require APPPATH . 'vendor/autoload.php'; // composer installing into application/vendor

// Use FCPATH If your .env is in the same directory as index.php
// Use APPATH If your .env is in your application/ directory
$dotenv = Dotenv\Dotenv::createMutable(APPATH);
$dotenv->load();

/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 */
require_once BASEPATH . 'core/CodeIgniter.php';