Skip to content

Change the folder layout of modules

Edit the paths section of config/modules.php to change where module:make and module:make-* place generated files. Publish the config file first if you have not already:

Terminal window
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag=config

To store module classes in src/ instead of app/, update paths.app_folder and every class generator path together.

  1. Set paths.app_folder.

    config/modules.php
    'paths' => [
    // ...
    'app_folder' => 'src/',
    ],
  2. Replace the app/ prefix with src/ on every class entry in paths.generator. Leave config, database, resources, routes, and tests unchanged.

    config/modules.php
    'generator' => [
    'actions' => ['path' => 'src/Actions', 'generate' => false],
    'controller' => ['path' => 'src/Http/Controllers', 'generate' => true],
    'provider' => ['path' => 'src/Providers', 'generate' => true],
    // ... update every other app/ entry
    ],

    If any class entry remains outside app_folder, module:make reports the invalid keys and stops before writing files.

  3. Create a module to verify the layout.

    Terminal window
    php artisan module:make Blog

    Classes are placed in Modules/Blog/src, and Modules/Blog/composer.json maps Modules\Blog\ to src/.

The class namespace excludes the app folder (Modules\Blog\Http\Controllers\BlogController lives in src/Http/Controllers).

To map the module namespace to the module root, set 'app_folder' => '' and drop the app/ prefix from class entries (for example 'provider' => ['path' => 'Providers', ...]). Every path segment must use StudlyCase.

Change the path of an individual generator entry:

config/modules.php
'controller' => ['path' => 'app/Http/Api/Controllers', 'generate' => true],

By default, the generated namespace follows the path (Modules\Blog\Http\Api\Controllers). To decouple the namespace from the path, add a namespace key (relative to the module namespace):

config/modules.php
'model' => ['path' => 'app/Domain/Models', 'generate' => false, 'namespace' => 'Models'],

module:make-model Post Blog now writes Modules/Blog/app/Domain/Models/Post.php in namespace Modules\Blog\Models. Map that custom namespace in the module’s composer.json and run composer dump-autoload:

Modules/Blog/composer.json
"autoload": {
"psr-4": {
"Modules\\Blog\\": "app/",
"Modules\\Blog\\Models\\": "app/Domain/Models/",
"Modules\\Blog\\Database\\Factories\\": "database/factories/",
"Modules\\Blog\\Database\\Seeders\\": "database/seeders/"
}
}

Toggle generate to control which folders module:make scaffolds for new modules:

config/modules.php
'jobs' => ['path' => 'app/Jobs', 'generate' => true],
'views' => ['path' => 'resources/views', 'generate' => false],

Individual module:make-* generators still write to the configured path even when generate is false. Setting routes, views, config, or assets to false also skips the starter files they own (see paths.generator).

  • Procedural helper files: list paths relative to the module root in the files array of module.json (for example "files": ["app/helpers.php"]). The package includes each file with include_once when registering enabled modules. See module.json.
  • Helper classes: run php artisan module:make-helper StringHelper Blog (or pass --invokable) to create a PSR-4 autoloaded class in app/Helpers.

Config changes apply only to new modules and future generator runs. Move existing folders manually, update autoload.psr-4 in the module’s composer.json, and run composer dump-autoload:

Terminal window
mv Modules/Blog/app Modules/Blog/src
Modules/Blog/composer.json
"Modules\\Blog\\": "src/",
Terminal window
composer dump-autoload

Create a module and inspect its file tree:

Terminal window
php artisan module:make Shop
find Modules/Shop -type f -not -name .gitkeep

The generated files match your configured paths.