【问题标题】:Why is my config item not populating from my getenv() entry in codeigniter?为什么我的配置项没有从我在 codeigniter 中的 getenv() 条目中填充?
【发布时间】:2016-12-11 16:41:31
【问题描述】:

我正在使用带有 Codeigniter 的 phpdotenv。 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

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

提前致谢。

【问题讨论】:

  • 尝试先加载你的配置,$this->load->config('your_config');。看看有没有帮助
  • 问题是 - config.php 在预系统挂钩之前被调用
  • @sintakonte 在 Codeigniter.php 中,它会在加载配置之前触发,据我所知?

标签: php codeigniter environment-variables


【解决方案1】:

正如@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';

【讨论】:

    【解决方案2】:

    哦,你基本上会这样做:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-21
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多