Customise the generator stubs
To change the code generated by module:make and module:make-*, publish the stubs and point the package at your copy. The package falls back to its built-in stub for any file missing from your copy.
-
Publish the stubs.
Terminal window php artisan vendor:publish --tag=stubsThis command copies the stubs to
stubs/nwidart-stubs. -
Publish
config/modules.phpif you have not already.Terminal window php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag=config -
Enable your published stubs in
config/modules.php.config/modules.php 'stubs' => ['enabled' => true,'path' => base_path('stubs/nwidart-stubs'),// ...],Configure the path here rather than calling the deprecated
FileRepository::setStubPath(), which does not affect generators. -
Edit a stub. For example, add
declare(strict_types=1);toenum.stub:stubs/nwidart-stubs/enum.stub <?phpdeclare(strict_types=1);namespace $CLASS_NAMESPACE$;enum $CLASS$ {}Keep the
$CLASS$and$CLASS_NAMESPACE$placeholders intact. -
Check the result.
Terminal window php artisan module:make-enum PostStatus BlogModules/Blog/app/Enums/PostStatus.phpnow begins withdeclare(strict_types=1);.
Change the files module:make writes
Section titled “Change the files module:make writes”stubs.files in config/modules.php maps each stub key to the destination path inside a new module.
Remove an entry to stop generating that file. For example, drop vite and package to skip Vite and npm files:
'files' => [ 'routes/web' => 'routes/web.php', 'routes/api' => 'routes/api.php', 'views/index' => 'resources/views/index.blade.php', 'views/master' => 'resources/views/components/layouts/master.blade.php', 'scaffold/config' => 'config/config.php', 'composer' => 'composer.json', 'assets/js/app' => 'resources/assets/js/app.js', 'assets/sass/app' => 'resources/assets/sass/app.scss',],To generate an extra file, create the stub, add it to stubs.files, and list its placeholders in stubs.replacements:
# $STUDLY_NAME$
The $LOWER_NAME$ module.'files' => [ // ... 'readme' => 'README.md',],'replacements' => [ // ... 'readme' => ['STUDLY_NAME', 'LOWER_NAME'],],Run php artisan module:make Shop and verify Modules/Shop/README.md exists.
If a stub key is omitted from stubs.replacements, module:make prints a warning and leaves its $TOKEN$ placeholders unreplaced. To define custom tokens, see Add your own stub placeholders.
Go back to the package stubs
Section titled “Go back to the package stubs”Set stubs.enabled to false so the package ignores stubs.path.
Before disabling custom stubs, remove custom stubs.files entries (such as readme) that exist only in your published folder. Otherwise module:make fails when trying to open them. Any custom generator that depends on a stub in your published folder fails the same way (see Add your own module commands and generators).
Changes to stubs.files remain active even when stubs.enabled is false. Restore removed entries like 'vite' and 'package' if you want them generated again.
Related
Section titled “Related”- Stubs and placeholders: the stub list, placeholders and lookup rules
stubsconfiguration keysvendor:publishtags- Add your own stub placeholders
- Add your own module commands and generators
- Generators and stubs: how a stub becomes a file, and why a published copy never changes by itself