13

I installed phpdotenv from vlucas using composer on a codeigniter project.

I have added the hook as well which I am bit confused if needed for v3.3

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

If I don't add this hook I can't retrieve variables from my .env file. If I do add it, then I get this error:

Message: Argument 1 passed to Dotenv\Dotenv::__construct() must be an instance of Dotenv\Loader, string given, called in C:\xampp\htdocs\test\application\config\hooks.php on line 15

Filename: C:\xampp\htdocs\test\vendor\vlucas\phpdotenv\src\Dotenv.php

Seems like the class is loading but it doesn't like the parameter "APPPATH" but all of the documentation I have found uses that.

Any help appreciated

miken32
  • 42,008
  • 16
  • 111
  • 154
mrsparrow
  • 371
  • 1
  • 2
  • 8

4 Answers4

11

Ok so changing that hook to this seems to be working, I am not entirely sure it's the correct approach but digging into the library code seems ok.

$hook['pre_system'] = function() {
    $dotenv = Dotenv\Dotenv::create(__DIR__);
    $dotenv->load();
}

If this is wrong for any reason please let me know. Thanks

Valor_
  • 3,461
  • 9
  • 60
  • 109
mrsparrow
  • 371
  • 1
  • 2
  • 8
  • 1
    You can still use `APPPATH` where you have `__DIR__` but yeah that's correct — phpdotenv got updated and this is the new way to do it. – catgofire Mar 09 '19 at 00:55
  • 1
    ::create didn't work for me, but as described in the package repository readme, `(Dotenv\Dotenv::createImmutable(__DIR__))->load();` this did work – jave.web Mar 03 '21 at 04:05
7

I tried all solutions then I found that my version of phpdotenv was 4.x.x. For those who have a confusion of why the solutions above doesn't work.

Here is the new way to load the env with path as the constructor param:

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__.'/..');
$dotenv->load();
Sam
  • 557
  • 1
  • 4
  • 19
  • 1
    Thanks! That worked! Since I was trying to use the dotenv with laravel envoy I had to remove the `'/..'` portions of the code because the envoy config file and the dotenv are both in the same directory. – Edu Ruiz Aug 14 '20 at 00:05
2

Dotenv must be an instance of Dotenv\Loader

Actually you are not following documentation of upgrading Laravel from any version to 5.8, I have found solution after searching of few hours. Finally i found the solution. You just need to replace this code in your environment file.

$env = $app->detectEnvironment(function(){
$environmentPath = __DIR__.'/../.env';
$setEnv = trim(file_get_contents($environmentPath));
if (file_exists($environmentPath))
{
    putenv('APP_ENV='.$setEnv);
    if (getenv('APP_ENV') && file_exists(__DIR__.'/../.' .getenv('APP_ENV') .'.env')) {
        $dotenv = Dotenv\Dotenv::create(__DIR__.'/../', '.'.getenv('APP_ENV').'.env');
        $dotenv->overload();
    }
}});

Here is a link where you can check in details how to use multiple env files in laravel 5.8. Reference Link

Enjoy the coding . . . !!!

Libra Ramis
  • 403
  • 3
  • 6
  • Whats with the putenv('APP_ENV='.$setEnv) etc ? I removed all of that to get mine to work . – Mike Q Jul 03 '19 at 02:47
0

do a composer show to see if the package is loaded. if not, delete your composer.lock file, composer install and check again. Somehow for some reason if you want to stay PHP 5, keep the version at 4 {"vlucas/phpdotenv": "4.1.0"}