Roots Discourse

oscarotero/env v2.1.0 Class 'Env' not found

I have recently updated composer and got the oscarotero/env v2.1.0 package. Since then I keep getting this error:

Fatal error: Uncaught Error: Class 'Env' not found in /myproject/config/application.php:6

This is the content of the referenced file, where Env::init() corresponds to line 6:

<?php

$root_dir = dirname(__DIR__);
$webroot_dir = $root_dir . '/public';

Env::init();

// Use Dotenv to set required environment variables and load .env file in root
$dotenv = new Dotenv\Dotenv($root_dir);
if (file_exists($root_dir . '/.env')) {
    $dotenv->load();
    try {
      $dotenv->required('DATABASE_URL')->notEmpty();
    } catch (Exception $e) {
      $dotenv->required(['DB_NAME', 'DB_USER', 'DB_PASSWORD', 'WP_HOME', 'WP_SITEURL']);
    }
}

It seems as if the package were not loaded since the Env Class doesn’t load. Ideas?

Try this:

use function Env\env;

See also: https://github.com/roots/bedrock/blob/master/config/application.php

I had the same problem and had a quick look at the current application.php in the bedrock repo.
TL;DR:
add Namespace use function Env\env; at the top
remove Env::init();
change Dotenv line to $dotenv = Dotenv\Dotenv::createImmutable($root_dir);