1

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

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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