2

I am trying to configure DotEnv in a project using CodeIgniter 3.

I see there is a composer.json in the root (outside application folder), so I run:

require vlucas/phpdotenv

I have enabled composer and hooks on config.php:

$config['composer_autoload'] = TRUE;
$config['enable_hooks'] = TRUE;

I have added:

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

I have created a .env file inside "application" folder.

My Problem

Vars are not loaded using $_ENV or getenv in database.php

What I have found

I need to run:

composer dump-autoload

If I change composer_autoload from TRUE to:

$config['composer_autoload'] = '[FULLPATH]/vendor/autoload.php';

It works!!

Documentation

According to documentation if we set composer_autoupload to true, it will call the autoload.php from:

application/vendor/autoload.php.

My Questions

  1. Do this mean, I have to move composer.json to application folder, so vendor is created inside of it?
  2. is it safe to use $_ENV
Eduardo
  • 1,781
  • 3
  • 26
  • 61
  • 1. Yes, docs are bit misleading for that one. Also, it would be much safer for you to keep composer out of public reach so no one could check what packages/libraries (and eventual leaks) you have installed. Therefore go with `true` and vendor inside `APPPATH` directory. 2. Safe from what? You can use `$_SERVER` and `getenv('VAR_KEY')` as well. – Tpojka Sep 07 '17 at 18:30
  • Thanks @Tpojka. Safe from injections. I know some vars in session can be injected – Eduardo Sep 07 '17 at 19:38
  • 1
    That means I should move composer.json inside application folder – Eduardo Sep 07 '17 at 19:39
  • Correct. Default directory meant to be residence for composer's vendor is `APPPATH` and you are good with using it that way. [Here](https://stackoverflow.com/questions/38813987/integrating-mailjet-api-v3-wrapper-as-codeigniter-library/38815612#38815612) you can see how I use it. – Tpojka Sep 07 '17 at 20:03
  • Tpojka, please write an answer to select it ;), thanks – Eduardo Sep 07 '17 at 20:26

0 Answers0