Fatal error: Class 'Dotenv\Dotenv' not found in
Asked Answered
K

12

11

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

Kenner answered 12/5, 2016 at 23:54 Comment(0)
Y
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.

Yettie answered 5/4, 2017 at 15:44 Comment(0)
P
6

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

Photosynthesis answered 3/10, 2019 at 6:48 Comment(0)
J
5

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();
Joella answered 21/2, 2022 at 15:11 Comment(0)
F
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.

Francophobe answered 26/10, 2016 at 14:1 Comment(1)
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": "[email protected]" }, { "name": "Germán Lena", "email": "[email protected]" } ] } Don't know how to fix it.Pathogenesis
G
1

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

Thanks

Guan answered 27/4, 2019 at 7:14 Comment(0)
S
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();
Socha answered 19/3, 2020 at 16:48 Comment(0)
F
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.

Friary answered 27/11, 2020 at 12:0 Comment(0)
R
0

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

Rubel answered 28/5, 2021 at 11:10 Comment(0)
V
0

install vlucas/phpdotenv by composer require vlucas/phpdotenv

Visitation answered 28/8, 2021 at 10:50 Comment(0)
M
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

Misha answered 10/6, 2022 at 13:38 Comment(0)
T
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!

Tracee answered 25/7, 2022 at 2:0 Comment(0)
O
0

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

Oriana answered 10/2, 2023 at 2:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.