Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error: Class 'Dotenv\Dotenv' not found in

Hello guys I am so confused I dont know what I am doing wrong this told me Fatal error: Class 'Dotenv\Dotenv' not found in

But I dont understand why..

$dotenv = new \Dotenv\Dotenv(dirname(dirname(dirname(dirname(__DIR__)))));
$dotenv->load();

My structure is the next and in the file index.php is where I am calling Dotenv also I used use Dotenv\Dotenv; but it doesnt work too.

enter image description here

like image 905
Marco Perez Avatar asked May 12 '16 23:05

Marco Perez


3 Answers

Be sure that you are using Dotenv after loading from vendor/autoload.php.

For example, I was using OpenCart, in which contained a file startup.php with:

// Autoloader
if (file_exists(DIR_VENDOR . 'autoload.php')) {
    require_once(DIR_VENDOR . 'autoload.php');
}

And I had defined DIR_VENDOR in config.php as:

define('DIR_VENDOR', __DIR__.'/vendor/');

So finally, in index.php, I would have:

// Startup
require_once(DIR_SYSTEM . 'startup.php');

// dotenv
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();

So startup.php loads vendor/autoload.php, which loads vlucas/phpdotenv, after which we can then find Dotenv\Dotenv.

like image 178
deltatango Avatar answered Nov 19 '22 22:11

deltatango


just remove/delete the vendor folder and reinstall with the -> composer install.

like image 45
pradeep prasanna rajapaksha Avatar answered Nov 20 '22 00:11

pradeep prasanna rajapaksha


check if you have "vlucas/phpdotenv" : "~2.2" in "require" tag in composer file. if you don't then add that plugin and open your terminal and run "composer dump-autoload" then run "composer update". and just to be safe run "composer dump-autoload" once again to refresh all the file paths.

and, if you do have phpdotenv plugin then add that plugin in "require" then just run the dump-autoload command.

like image 2
sunder Avatar answered Nov 19 '22 22:11

sunder