3

First attempt at making a module. So far all I've done is try to change the namespace of the template that was included with my freshly installed craft 3.0.23.1 project.

Was following the official docs (https://docs.craftcms.com/v3/extend/module-guide.html#set-up-the-basic-file-structure) and spend a few hours trying to figure this out on various searches here and elsewhere.

So in the file '/modules/Module.php' I changed

namespace modules;

to

namespace banana\modules;

, then in '/composer.json' changed:

  "autoload": {
    "psr-4": {
      "modules\\": "modules/"
    }
  },

to

  "autoload": {
    "psr-4": {
      "banana\\modules\\": "modules/"
    }
  },

Then in Terminal, ran the command 'composer dump-autoload -a' in the project folder. Reload the page and I get the error. Other than just doing the setup wizard, i haven't touched any other files in the project.

2 Answers2

2

It's a bit difficult without seeing all of your code, but I'm going to guess that you also need to make changes in your config/app.php to make the namespace changes you're making.

So two things I'd recommend... first, you can generate your module's framework here: https://pluginfactory.io/

...and second I wrote up a tutorial on creating a module in the Enhancing a Craft CMS 3 Website with a Custom Module article.

andrew.welch
  • 11,551
  • 22
  • 31
  • 1
    Ah, the problem was when i set the Module ID in 'config/app.php'. I thought the value of the 'class' key was using the path to the class file. But I see it's actually '{namespace}{class name}::class'.

    Thanks @andrew.welch for pointing me in the right direction.

    – MrMagicalBananaful Sep 10 '18 at 18:00
  • I added it to the config/app.php file, as well as the composer.json and forgot to run composer update – Brad Feb 21 '19 at 20:10
1

I ran into this problem myself recently. It turned out to be a autoloader problem. I had forgotten to run composer dump-autoload on my project directory. I generally don't bother editing the composer autoload file manually.