1

Ran upgrade on my local dev environment. Seemed to go fine. Front end site broke and command line updates for craft also error.

My index.php looks like

// Set path constants
define('CRAFT_BASE_PATH', dirname(__DIR__));
define('CRAFT_VENDOR_PATH', CRAFT_BASE_PATH.'/vendor');

// Load Composer's autoloader
require_once CRAFT_VENDOR_PATH.'/autoload.php';

// Load dotenv?
if (file_exists(CRAFT_BASE_PATH.'/.env')) {
    (new Dotenv\Dotenv(CRAFT_BASE_PATH))->load();
}

// Load and run Craft
define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');
$app = require CRAFT_VENDOR_PATH.'/craftcms/cms/bootstrap/web.php';
$app->run();

CLI craft update gives me:

 Uncaught TypeError: Argument 1 passed to Dotenv\Dotenv::__construct() 
...vendor/vlucas/phpdotenv/src/Dotenv.php:60

I tried downgrading vulcas DotEnv package with no luck. I then set craft requirement in compose to 3.4.0 but still getting issues loading and updating on CLI, all centered around the Dotenv package.

I did a vanilla install via CLI to check index.php content which are different - AND i noticed composer.json states craft requires php8! Not what the docs say but thats another post.

Any help appreciated

1 Answer 1

4

For vlucas/phpdotenv for 3.4.0, this index.php template should work:

<?php
/**
 * Craft web bootstrap file
 */

// Set path constants
define('CRAFT_BASE_PATH', dirname(__DIR__));
define('CRAFT_VENDOR_PATH', CRAFT_BASE_PATH.'/vendor');

// Load Composer's autoloader
require_once CRAFT_VENDOR_PATH.'/autoload.php';

// Load dotenv?
if (class_exists('Dotenv\Dotenv') && file_exists(CRAFT_BASE_PATH.'/.env')) {
    Dotenv\Dotenv::create(CRAFT_BASE_PATH)->load();
}

// Load and run Craft
define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');
$app = require CRAFT_VENDOR_PATH.'/craftcms/cms/bootstrap/web.php';
$app->run();

For console:

#!/usr/bin/env php
<?php
/**
 * Craft console bootstrap file
 */

// Set path constants
define('CRAFT_BASE_PATH', __DIR__);
define('CRAFT_VENDOR_PATH', CRAFT_BASE_PATH.'/vendor');

// Load Composer's autoloader
require_once CRAFT_VENDOR_PATH.'/autoload.php';

// Load dotenv?
if (class_exists('Dotenv\Dotenv') && file_exists(CRAFT_BASE_PATH.'/.env')) {
    Dotenv\Dotenv::create(CRAFT_BASE_PATH)->load();
}

// Load and run Craft
define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');
$app = require CRAFT_VENDOR_PATH.'/craftcms/cms/bootstrap/console.php';
$exitCode = $app->run();
exit($exitCode);

It is the Dotenv\Dotenv::create(CRAFT_BASE_PATH)->load(); line that allows it to work. Repeat for your craft console file.

You can use the 5.x.x version of vlucas/phpdotenv, but it does require changes to how it is loaded compared to 3.4.x

The Craft CMS starter project provides a 5.x.x example, however this is using a bootstrap include method across both web and console, so you'd have to refactor slightly, but worth it as you don't have to duplicate the same logic in the your console file anymore.

https://github.com/craftcms/craft/blob/main/bootstrap.php

6
  • There's a separate console file???
    – joomkit
    Commented Jun 27, 2022 at 19:34
  • Dotenv is loaded on both the web/index.php and craft file at the root, so you'll need to update both to the necessary Dotenv loading syntax relative to the version in use. If you look at the Craft starter project, they use a bootstrap include file across both so you don't have to duplicate it. github.com/craftcms/craft Commented Jun 27, 2022 at 21:03
  • Thanks James. I still don't get what the issue is. I have upgraded another site on local dev with no issues - they have the same craft and phpdotenv versions in their respective composer.json. Both load frontend fine. But one will not run craft commands on command line. Last ditch attempt i swapped the craft shell file from the working to the broken - hey presto. I realised thats what you call the console file? #still-learning thx
    – joomkit
    Commented Jun 29, 2022 at 14:39
  • Yes the craft file at the root is the PHP wrapper file for calling Craft console commands with ./craft. This was likely using the wrong dotenv syntax for loading based on the version. Commented Jun 29, 2022 at 21:40
  • Yes that was it
    – joomkit
    Commented Jun 30, 2022 at 12:35

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.