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:
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag=configRename the class folder
Section titled “Rename the class folder”To store module classes in src/ instead of app/, update paths.app_folder and every class generator path together.
-
Set
paths.app_folder.config/modules.php 'paths' => [// ...'app_folder' => 'src/',], -
Replace the
app/prefix withsrc/on every class entry inpaths.generator. Leaveconfig,database,resources,routes, andtestsunchanged.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:makereports the invalid keys and stops before writing files. -
Create a module to verify the layout.
Terminal window php artisan module:make BlogClasses are placed in
Modules/Blog/src, andModules/Blog/composer.jsonmapsModules\Blog\tosrc/.
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.
Move one folder
Section titled “Move one folder”Change the path of an individual generator entry:
'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):
'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:
"autoload": { "psr-4": { "Modules\\Blog\\": "app/", "Modules\\Blog\\Models\\": "app/Domain/Models/", "Modules\\Blog\\Database\\Factories\\": "database/factories/", "Modules\\Blog\\Database\\Seeders\\": "database/seeders/" }}Choose the folders module:make creates
Section titled “Choose the folders module:make creates”Toggle generate to control which folders module:make scaffolds for new modules:
'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).
Load helper files
Section titled “Load helper files”- Procedural helper files: list paths relative to the module root in the
filesarray ofmodule.json(for example"files": ["app/helpers.php"]). The package includes each file withinclude_oncewhen registering enabled modules. Seemodule.json. - Helper classes: run
php artisan module:make-helper StringHelper Blog(or pass--invokable) to create a PSR-4 autoloaded class inapp/Helpers.
Update modules that already exist
Section titled “Update modules that already exist”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:
mv Modules/Blog/app Modules/Blog/src"Modules\\Blog\\": "src/",composer dump-autoloadCheck the result
Section titled “Check the result”Create a module and inspect its file tree:
php artisan module:make Shopfind Modules/Shop -type f -not -name .gitkeepThe generated files match your configured paths.
Related
Section titled “Related”pathsconfiguration keysmodule.jsonmodule:makeandmodule:make-helper- Generator commands: path and namespace
- Change where modules live and their namespace
- Create a module
- Anatomy of a module: how the namespace maps to the app folder