Skip to content

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.

  1. Publish the stubs.

    Terminal window
    php artisan vendor:publish --tag=stubs

    This command copies the stubs to stubs/nwidart-stubs.

  2. Publish config/modules.php if you have not already.

    Terminal window
    php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag=config
  3. 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.

  4. Edit a stub. For example, add declare(strict_types=1); to enum.stub:

    stubs/nwidart-stubs/enum.stub
    <?php
    declare(strict_types=1);
    namespace $CLASS_NAMESPACE$;
    enum $CLASS$ {}

    Keep the $CLASS$ and $CLASS_NAMESPACE$ placeholders intact.

  5. Check the result.

    Terminal window
    php artisan module:make-enum PostStatus Blog

    Modules/Blog/app/Enums/PostStatus.php now begins with declare(strict_types=1);.

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:

config/modules.php
'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:

stubs/nwidart-stubs/readme.stub
# $STUDLY_NAME$
The $LOWER_NAME$ module.
config/modules.php
'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.

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.