How can I use Environment Variables in the Codeigniter Config file?

Viewed 654

How can I use Environment variables to set up the encryption key in the config file for codeigniter?

I have set up a dotenv library and environment variables work everywhere else apart from the config file. I'm getting an empyty string.

$config['encryption_key'] = getenv("APP_key");

1 Answers

After a very long time, I noticed that using hooks could help: So in application/config/hooks.php we write something like so:

$hook['post_controller_constructor'][] = array(
    'class' => '',
    'function' => 'set_appkey',
    'filename' => 'appkey.php',
    'filepath' => 'hooks'
);

and create a new file: appkey.php in application/hooks with the function

function set_appkey()
{
    $CI =& get_instance();
    $CI->config->set_item('encryption_key', getenv("APP_KEY"));
}

and finally, in config.php, we leave the encryption_key variable blank

$config['encryption_key'] = '';
Related