ddev: Don't manage Craft 3 .env, Fix `*_SITE_URL`

The recently added settings management for Craft projects broke my ddev / Craft 4 project - when I came back to it after some time away - and in a way I couldn’t immediately understand from this error:

Failed to start craft-dev: template: templateStringToFile:/home/jeremy/ddev-projects/c4-dev/.env:7: function "siteUrl" not defined

Prior to this the project was working fine for a few months, it was only on coming back to it that the new settings management kicked in, with a ddev upgrade.

Thus, I think disable_settings_management should definitely default to false for existing projects.

As per the other issue discussion, I think PRIMARY_SITE_URL should be added only if the existing & typical CRAFT_SITE_URL is not already present - otherwise it’s just doubling up right?

The config setting is the default way to define this in Craft: https://craftcms.com/knowledge-base/how-craft-determines-the-site-url https://craftcms.com/docs/2.x/config-settings.html#siteurl

And thus, with Craft 4’s new approach to .env variables, CRAFT_SITE_URL is the de facto default way of actually implementing this.

Personally I am not super fussed about Mailhog, other than to note that Craft has a config setting for a similar thing already: https://docs.craftcms.com/v3/config/config-settings.html#testtoemailaddress

        'testToEmailAddress' => (\craft\helpers\App::env('SEND_ALL_EMAILS_TO')) ?? null,

So in Craft’s case, Mailhog is likely to be irrelevant to most and I would expect that to be set as a matter of course in .env files for dev environments.

_Originally posted by @bossanova808 in https://github.com/drud/ddev/issues/4552#issuecomment-1429132626_

(Adding as a new issue per request)

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 19 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Ok great, thanks for weighing in! So it sounds like we should ditch any mention of PRIMARY_SITE_URL or CRAFT_SITE_URL, and run with this instead…

CRAFT_WEB_URL="https://projectname.ddev.site"
CRAFT_WEB_ROOT="/var/www/html/web"

For Craft 4, of course. None of these would be applied to a Craft 3 site.

Correct me if I’m mistaken @timkelty!

I’m not a composer expert, but I saw @khalwat mention this on Discord…

composer show craftcms/cms

Would that help us determine the current version of Craft?

Looks like this command returns a JSON of the package data from the composer.lock file…

ddev composer show craftcms/cms --locked --format=json

Sorry for the late response! You (accidentally) raise a good philosophical question @rfay

Should these two environment variables even be set in a local environment?

I lean towards “yes”, for the following reasons:

  • Convenience. These values can (and frequently do) feed into other values that get parsed out via PHP. There are several common Craft methods (like url) which attempt to use these values if they exist.
  • General security practices. Since it’s important for these values to exist in every other environment, it may be good practice for people to be accustomed to seeing them as part of the .env file. It’s pretty common to copy an .env file between environments, then simply change the relevant values.

As usual, I’ll defer to Tim (or another authority) if I’m mistaken about any of this!

For Craft 4 - PRIMARY_SITE_URL is not right, I think. As per the others, CRAFT_SITE_URL would be the standard there

This isn’t quite right. There is in fact, no “standard” for this variable, because in Craft 4 there is no siteUrl config setting (because multi-site). Setting this env wouldn’t magically do anything unless you manually referenced it in your site’s URL. In that regard, naming it CRAFT_* is actually a bit misleading.

In fact… according to https://github.com/craftcms/cms/pull/11912, the CRAFT_WEB_URL and CRAFT_WEB_ROOT were created for this explicit purpose. Perhaps we should be using those instead?

@lindseydiloreto this is exactly what I would suggest. Since a DDEV install is always going to “single-site” by default, setting these in env allows you to just set your primary site’s URL to @web and you’re all set.