Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dotenv must be an instance of Dotenv\Loader

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

like image 484
mrsparrow Avatar asked Feb 28 '19 08:02

mrsparrow


1 Answers

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();
like image 65
Sam Avatar answered Oct 01 '22 18:10

Sam