5

Just updated to Craft 4, migrated my /web folder over as well as my /assets folder and trying to load the index page gives me the following error.

Any help would be great as I can no longer access my /admin

Fatal error: Uncaught TypeError: Dotenv\Dotenv::create(): Argument #1 ($repository) must be of type Dotenv\Repository\RepositoryInterface, string given, called in /app/web/index.php on line 15 and defined in /app/vendor/vlucas/phpdotenv/src/Dotenv.php:83 Stack trace: #0 /app/web/index.php(15): Dotenv\Dotenv::create('/app') #1 {main} thrown in /app/vendor/vlucas/phpdotenv/src/Dotenv.php on line 83

My composer.json looks like the following:

{
  "require": {
    "craftcms/cms": "^4.2.0.2",
    "craftcms/feed-me": "5.0.4",
    "craftcms/redactor": "3.0.2",
    "nystudio107/craft-seomatic": "4.0.7",
    "nystudio107/craft-similar": "4.0.0",
    "putyourlightson/craft-blitz": "4.2.1",
    "putyourlightson/craft-sprig": "2.1.0",
    "verbb/image-resizer": "3.0.1",
    "verbb/wishlist": "2.0.0",
    "vlucas/phpdotenv": "^5.4.0"
  },
  "require-dev": {
    "yiisoft/yii2-shell": "^2.0.3"
  },
  "autoload": {
    "psr-4": {
      "modules\\": "modules/"
    }
  },
  "config": {
    "allow-plugins": {
      "craftcms/plugin-installer": true,
      "yiisoft/yii2-composer": true
    },
    "sort-packages": true,
    "optimize-autoloader": true,
    "platform": {
      "php": "8.1.0"
    }
  },
  "scripts": {
    "post-root-package-install": [
      "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ]
  }
}

1 Answer 1

7

Looks like your web/index.php file was created for an older version of the vlucas/phpdotenv library. The error method shows that you're initializing the library in a way that's compatible with version ^3.0.0, but in your composer.json, you're requiring ^5.4.0. In this version, the library is initialized in a different way, so you need to adjust your web/index.php accordingly.

If you're looking for a good place to start, check out the way the library is initialized in the current version of the Craft starter project:

// Load dotenv?
if (class_exists(Dotenv\Dotenv::class)) {
    // By default, this will allow .env file values to override environment variables
    // with matching names. Use `createUnsafeImmutable` to disable this.
    Dotenv\Dotenv::createUnsafeMutable(CRAFT_BASE_PATH)->safeLoad();
}

You're gonna have the same problem with the craft binary used for console commands. I recommend copying the following files from the starter project to your local project:

  • bootstrap.php
  • craft
  • web/index.php
2
  • unfortunately the link does not work anymore and if I go to the 4.x version these files don't have any dotenv parts in them... Example: github.com/craftcms/craft/blob/4.x/craft do you know if things have further changed?
    – Merc
    Commented Nov 6 at 15:30
  • @Merc The link is now up-to-date again! Didn't work any more because the main branch was replaced by a 5.x branch. The craft binary is very minimal, because it shares the initialization code with web/index.php. The code you're looking for is in bootstrap.php. See the updated link above for an example.
    – MoritzLost
    Commented Nov 6 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.