1

I have created a module in modules/Core.php which works great and I can reference it using modules\Core::class. But I want to re-organise my modules.

I'm not super familiar with PSR-4 loading but I have taken a look at a bunch of modules to try and copy the file structure and composer file.

Here's the file structure I want:

modules
  └── wrux
    └── Core
      ├── README.md
      ├── composer.json
      └── src
        └── Core.php

Here's my modules/wrux/Core/composer.json file:

{
  "name": "wrux/Core",
  "require": {
    "craftcms/cms": "^3.2.0"
  },
  "autoload": {
    "psr-4": {
      "wrux\\Core\\": "src/"
    }
  }
}

After I run composer dump-autoload I was hoping that I can use the wrux\Core namespace but it's not working. My module is only accessible from then modules\wrux\Core\src namespace.

Any ideas here?

1
  1. I don't think you need to add any composer.json in your module folders
  2. You have to update the project composer.json file only to let the craft know about custom module so that it can load on each boot.
  3. Folder Structure might be like (inside module folder at the root)

wruxmodule
└── src
    ├── controllers
    │   └── DefaultController.php
    ├── models
    │   └── WruxModuleModel.php
    ├── variables
    │   └── WruxModuleVariable.php
    └── WruxModule.php
  1. Copy below line to config/app.php

 return [
    'modules' => [
        'wrux-module' => [
            'class' => \modules\wruxmodule\WruxModule::class,
        ],
    ],
    'bootstrap' => ['wrux-module'],
];
  • Now update the project composer.json file

"autoload": {
    "psr-4": {
      "modules\\wruxmodule\\": "modules/wruxmodule/src/"
    }
},
  • Finally run

composer dump-autoload ( at project root)
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.