craftcms / generator

Scaffold new Craft CMS plugins, modules, and system components from the CLI
MIT License
83 stars 8 forks source link

Can't create module inside modules/ in own folder? #37

Closed mandrasch closed 4 weeks ago

mandrasch commented 1 month ago

Description

I guess I'm missing something - but it seems not to be possible to create a new module folder inside the modules/ folder with craft make module? 🤔

I would like to have this structure:

image

Steps to reproduce

  1. ddev craft make module
  2. Input:
Module ID: (kebab-case) my-custom-module 
Base module class name: (PascalCase) [Module] MyCustomModule
Module location: modules/my-custom-module/

Error: That directory would conflict with the existing modules\ autoload root (modules\my-custom-module isn’t a valid PHP namespace).

Using Module location: modules/ works, but then the module has no folder on its own.

Thanks in advance!

Additional info

Related docs: https://craftcms.com/docs/5.x/extend/module-guide.html

brandonkelly commented 4 weeks ago

It’s saying that you already have an autoload root defined in composer.json that points to your modules/ folder directly. So all directories inside it need to be valid PHP namespace segments, e.g. modules/mymoduleid (no dashes).

If you don’t have any existing PHP code within modules/, it’s safe to just remove the autoload entry from composer.json and try again.

mandrasch commented 4 weeks ago

Hi, thanks very much for quick reply and assistance @brandonkelly!

The autoload is already created by a fresh install of CraftCMS

  "minimum-stability": "dev",
  "prefer-stable": true,
  "require": {
    "craftcms/cms": "^5.0.0",
    "vlucas/phpdotenv": "^5.4.0"
  },
  "require-dev": {
    "craftcms/generator": "^2.0.0",
    "yiisoft/yii2-shell": "^2.0.3"
  },
  "autoload": {
    "psr-4": {
      "modules\\": "modules/"
    }
  },

The structure of subfolders is mentioned in the docs, therefore I was confused why the docs don't show an example for it.

I just tried with

ddev craft make module
Module ID: (kebab-case) my-custom-module
Base module class name: (PascalCase) [Module] MyCustomModule
Module location: modules/mycustommodule    

Should the module be loaded during app initialization? (yes|no) [no]:yes
 → Creating modules/mycustommodule/ … ✓
 → Creating modules/mycustommodule/MyCustomModule.php … ✓
 → Updating config/app.php … ✓

✅ Module created!

image

Is this the correct approach you meant?

It would be very helpful to have this in the docs imho - happy to propose a quick PR for it!

timkelty commented 3 weeks ago

The autoload is already created by a fresh install of CraftCMS

Good point. That's now been removed in the latest version of the composer project: https://github.com/craftcms/craft/releases/tag/5.1.0

brandonkelly commented 3 weeks ago

@mandrasch We meant to remove that default autoload root alongside launching Generator, but I guess it was overlooked, so as Tim said, it’s gone now for new installs.

Had that not been there, you would end up with a separate autoload root for each of your modules, rather than just one for the root modules/ folder, and then you wouldn’t have gotten that error.

If you’re fine with the current folder structure, then it’s fine to leave it as-is. But if you want to switch to a kebab-cased module folder for your new module, you would need to manually adjust the autoload array in composer.json, listing each module’s namespace root individually, at which point the name of the module folders won’t matter.

mandrasch commented 3 weeks ago

Thanks very much for details @brandonkelly & @timkelty!