Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
mandrasch opened this issue Aug 20, 2024 · 5 comments
Closed

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

mandrasch opened this issue Aug 20, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@mandrasch
Copy link

mandrasch commented Aug 20, 2024

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

  • Craft version: 5.3.4
  • PHP version: 8.2
  • Database driver & version: MySQL 8.0
  • Plugins & versions: none

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

@mandrasch mandrasch added the bug Something isn't working label Aug 20, 2024
@brandonkelly
Copy link
Member

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
Copy link
Author

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
Copy link

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
Copy link
Member

@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
Copy link
Author

mandrasch commented Aug 29, 2024

Thanks very much for details @brandonkelly & @timkelty!

timkelty referenced this issue in craftcms/craft Sep 3, 2024
timkelty referenced this issue in craftcms/craft Sep 3, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants