1

I'm working on a Craft site for a company that wanted me to change the usual folder structure of the project. Instead of it being

-config
-storage
-templates
…
-public

they wanted to group all the folders and files related to Craft itself into a separate folder so the folder structure would be like this

-craft
--config
--storage
--templates
…
-public

because they have a lot of folders for other things in the root of the server and wanted to be able to easily see which ones were related to Craft.

Everything is working fine except that in my local environment, I'm running DDEV so it looks like this:

-.ddev
-craft
-public

and to run composer or php craft console commands, I have to cd into the craft folder because that's where the Craft console bootstrap file and composer.json are.

composer commands work OK but any php craft commands always return

Craft can’t connect to the database. Check your connection settings.

which I think is because the .ddev folder is in the level above.

So I can't currently run any Craft console commands which I'm able to get around most of the time. Plugin updates I can do from the control panel and even migrations seem to complete properly most of the time by just visiting the control panel and then clicking the button to perform database updates.

But now I need to redo a migration that didn't complete properly before and I'm unable to.

So I need advice on how to get php craft commands to work with the way I have the project structured. Or else how I can get the migration I missed to run without using the CLI.

3 Answers 3

3

You already know using a non-standard folder structure is going to cause pain, but here we.

If the site is working, but the ddev craft command is not, let's override the command.

  • Add a new file: .ddev/commands/web/craft
  • Add the following to the file
#!/bin/bash

## Description: Run Craft CMS command inside the web container
## Usage: craft [flags] [args]
## Example: "ddev craft db/backup" or "ddev craft db/backup ./my-backups" (see https://craftcms.com/docs/4.x/console-commands.html)
## ProjectTypes: craftcms,php
## ExecRaw: true

if [ "${DDEV_PROJECT_TYPE}" != "craftcms" ]; then
    echo "The craft command is only available in the craftcms project type. You can update this in your project's config file, followed by restarting the DDEV project."
else
    CRAFT_CMD_ROOT=/var/www/html/craft

    cd "${CRAFT_CMD_ROOT}"
    php craft "$@"
fi

This is a copy of the global command, but will override it because its on the project level.

  • I removed the #ddev-generated comment so DDEV doesn't override it.
  • hard-coded CRAFT_CMD_ROOT=/var/www/html/craft directory where the "Craft console bootstrap file" (craft) file is. Eg. "/var/www/html/craft/craft".
0
3

Change the working_dir for the web container:

ddev config --web-working-dir=/var/www/html/craft

Or manually edit .ddev/config.yaml:

type: craftcms
docroot: public
...
working_dir:
    web: /var/www/html/craft
1

regarding DDEV - you can adjust composer root to a subdirectory:

https://ddev.readthedocs.io/en/stable/users/configuration/config/#composer_root

For your errors:

Craft can’t connect to the database. Check your connection settings.

DDEV assumes the original folder structure of craftcms and only handles .env automatically in the root folder.

DDEV replaces DB_HOST, PASSWORD, DB NAME etc. with "db" on start automagically, but doesn't know about your /craft/.env file.

You need to do this manually now in your case.

I don't know if it's a good idea to move craftcms to a subdirectory, I would advise against and stick to the official way of doing things.

If you need further help, you can also get support in DDEV discord (https://discord.gg/5wjP76mBJD) if it is DDEV-related.

But most likely DDEV team will also advise you to stick to the standard structure to get the most out of craftcms and DDEV - e.g. automatic .env handling for DDEV.

But it can be done of course, I would recommend checking out a .env from a normal craft project started with DDEV, e.g.:

# Database connection settings for DDEV
CRAFT_DB_DRIVER="mysql"
CRAFT_DB_SERVER="db"
CRAFT_DB_PORT="3306"
CRAFT_DB_DATABASE="db"
CRAFT_DB_USER="db"
CRAFT_DB_PASSWORD="db"
CRAFT_DB_SCHEMA=public
CRAFT_DB_TABLE_PREFIX=

PRIMARY_SITE_URL="https://ddev-craftcms-vite.ddev.site"

Cheers!

1
  • Composer commands I don't have a problem with, it's Craft CLI commands. I can't change the folder structure, it's the way the clients want their server set up. I'll post on DDEV Discord to see if they can help.
    – Tyssen
    Commented Jul 26 at 5:49

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.