9

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

miken32
  • 42,008
  • 16
  • 111
  • 154
Marco Perez
  • 160
  • 1
  • 1
  • 6

13 Answers13

9

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.

deltatango
  • 166
  • 1
  • 6
6

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

4

with "vlucas/phpdotenv": "^5.4" it now works like this:

<?php
require 'vendor/autoload.php';

use Dotenv\Dotenv;

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

or

<?php
require 'vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__); //Notice the Namespace and Class
$dotenv->load();
Pierre
  • 8,397
  • 4
  • 64
  • 80
2

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.

sunder
  • 21
  • 2
  • I'm having same problem. Here is composer file. `{ "name": "auth0/basic-webapp-sample", "description": "Basic sample for securing a WebApp with Auth0", "require": { "vlucas/phpdotenv": "2.4.0", "auth0/auth0-php": "~5.0" }, "license": "MIT", "authors": [ { "name": "Martin Gontovnikas", "email": "martin@gon.to" }, { "name": "Germán Lena", "email": "german.lena@gmail.com" } ] }` Don't know how to fix it. – Musaddiq Khan Apr 04 '19 at 16:25
1

You just need to remove complied.php from bootstrap\cache and it will works fine.

Thanks

Vishal Ribdiya
  • 840
  • 6
  • 18
1

For me worked this code:

use Dotenv\Dotenv;

require __DIR__ . '/../vendor/autoload.php';

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

Instead of this:

require __DIR__ . '/../vendor/autoload.php';

$dotenv = new Dotenv/Dotenv(__DIR__);
$dotenv->load();
Andrej
  • 196
  • 2
  • 9
0

I'm using the PhpStorm IDE and I installed dotenv on the server via SSH. Subsequently I didn't have the new vendor files in the local directory which caused this error. I simply downloaded the up-to-date vendor folder from the server (overwriting the local one) and the error went away.

wkille
  • 543
  • 6
  • 12
0

I just deleted vlucas and run composer install again. That solved it for me

Skywalker
  • 1,717
  • 1
  • 22
  • 25
0

install vlucas/phpdotenv by composer require vlucas/phpdotenv

ytdm
  • 1,069
  • 1
  • 12
  • 15
0

Because you have composer installed

$ composer require vlucas/phpdotenv

if you arent sure about how to $ composer require vlucas/phpdotenv try download from github here

0

What helped me:

  1. Delete vendor folder fully;
  2. Delete vlucas/phpdotenv library from a composer.json;
  3. Delete composer.lock itself;
  4. Run composer install to initiate again vendor folder and composer.lock file;
  5. Run composer require vlucas/phpdotenv again to download the library.

It may still say Undefined type 'Dotenv\Dotenv'.intelephense(1009) on your code editor but on your localhost your project should load without an error. Hope it helps!

Karina
  • 57
  • 1
  • 9
0

Make sure your path to autoload.php is correct '../vendor/autoload.php';

-1

use Dotenv\Dotenv;

require DIR . '/../vendor/autoload.php';

$dotenv = new Dotenv(DIR . "/.."); $dotenv->load();

This is also worked for me. We can Use this.Thank you.

msp
  • 1
  • 1
  • This is simply a copy of another answer here (https://stackoverflow.com/a/60761599/3511695), but unformatted – velkoon Apr 19 '21 at 09:34