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

<hr>

<pre><code>wruxmodule
└── src
    ├── controllers
    │   └── DefaultController.php
    ├── models
    │   └── WruxModuleModel.php
    ├── variables
    │   └── WruxModuleVariable.php
    └── WruxModule.php
</code></pre>

<ol>
<li>Copy below line to <code>config/app.php</code></li>
</ol>

<hr>

<pre><code> return [
    'modules' =&gt; [
        'wrux-module' =&gt; [
            'class' =&gt; \modules\wruxmodule\WruxModule::class,
        ],
    ],
    'bootstrap' =&gt; ['wrux-module'],
];
</code></pre>

<ul>
<li>Now update the project <code>composer.json</code> file</li>
</ul>

<hr>

<pre><code>"autoload": {
    "psr-4": {
      "modules\\wruxmodule\\": "modules/wruxmodule/src/"
    }
},
</code></pre>

<ul>
<li>Finally run</li>
</ul>

<hr>

<pre><code>composer dump-autoload ( at project root)
</code></pre>

CC BY-SA 4.0
2019-09-21T18:59:28.497Z
2019-09-21T18:59:28.497Z
inrsaurabh
<p>I have created a module in <code>modules/Core.php</code> which works great and I can reference it using <code>modules\Core::class</code>. But I want to re-organise my modules. </p>

<p>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.</p>

<p>Here's the file structure I want:</p>

<pre><code>modules
  └── wrux
    └── Core
      ├── README.md
      ├── composer.json
      └── src
        └── Core.php
</code></pre>

<p>Here's my <code>modules/wrux/Core/composer.json</code> file:</p>

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

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

<p>Any ideas here?</p>

<modules><composer>
PS-4 Autloading custom module
# PS-4 Autloading custom module

(Asked by: username_0 on 2019-09-21)

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?

**Tags:** modules, composer

---

## 1 answers

### Answer by username_1 on 2019-09-21. Score: 3

I don't think you need to add any composer.json in your module folders
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.
Folder Structure might be like  (inside module folder at the root)


wruxmodule
└── src
    ├── controllers
    │   └── DefaultController.php
    ├── models
    │   └── WruxModuleModel.php
    ├── variables
    │   └── WruxModuleVariable.php
    └── WruxModule.php


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)


---

