Reputation: 1656
I am using phpdotenv with Codeigniter. Codeigniter's environment setup doesn't quite work for this project.
I'm trying to set this in my config.php file:
$config['site_id'] = getenv('APP_ID');
phpdotenv is being loaded via the pre_system hook and getenv('APP_ID') is available throughout the app. I've also checked in the core and this fires well before loading config items.
$hook['pre_system'] = function() {
$dotenv = new Dotenv\Dotenv(APPPATH);
$dotenv->load();
};
The value of $this-config->item('site_id')
is always NULL
Any advice as to why this is happening would be really appreciated.
Thanks in advance.
Upvotes: 5
Views: 5201
Reputation: 154
As @jhodgson4 stated, CodeIgniter loads the configured constants first during the bootstrap of the framework (core/CodeIgniter.php
). You can require and intialize dotenv at the bottom of your index.php
file, before CodeIgniter.php is loaded. This should allow you to use getenv in any of your config files.
// ...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';
Upvotes: 1
Reputation: 1
Oh, you would basically just do this :
require APPPATH . 'vendor/autoload.php';
$dotenv = new Dotenv\Dotenv(BASEPATH . '../');
$dotenv->load();
Upvotes: 0