Skip to content

Add your own stub placeholders

To compute a custom placeholder such as $TABLE_PREFIX$ when module:make renders stubs, create a replacement class and register it in stubs.replacements. Prerequisites: an existing Blog module, plus published and enabled stubs (complete steps 1–3 of Customise the generator stubs).

  1. Generate the replacement class in Blog.

    Terminal window
    php artisan module:make-replacement TablePrefix Blog

    This creates Modules/Blog/app/Console/Replacements/TablePrefix.php.

  2. Update handle() to return the replacement value. The generated method returns the lowercase name of the module being created; append an underscore for a table prefix.

    Modules/Blog/app/Console/Replacements/TablePrefix.php
    <?php
    namespace Modules\Blog\Console\Replacements;
    use Nwidart\Modules\Support\ReplacementKeyCommand;
    class TablePrefix extends ReplacementKeyCommand
    {
    public function handle(): string
    {
    return strtolower($this->generator->getName()).'_';
    }
    }
  3. Register the class under the stub key in config/modules.php. Use an uppercase token name as the key and keep the existing built-in tokens.

    config/modules.php
    'replacements' => [
    // ...
    'scaffold/config' => [
    'STUDLY_NAME',
    'TABLE_PREFIX' => \Modules\Blog\Console\Replacements\TablePrefix::class,
    ],
    ],
  4. Insert the token in the published stub.

    stubs/nwidart-stubs/scaffold/config.stub
    <?php
    return [
    'name' => '$STUDLY_NAME$',
    'table_prefix' => '$TABLE_PREFIX$',
    ];
  5. Check the result by generating a new module and inspecting its config file.

    Terminal window
    php artisan module:make Shop
    cat Modules/Shop/config/config.php

    The file contains 'table_prefix' => 'shop_',.

The returned string is inserted verbatim. For JSON stubs (module.json and composer.json), escape backslashes and quotes yourself (\\ and \"); invalid JSON aborts module:make before writing files.

Replacement classes apply only to stubs rendered by module:make (stubs.files and json), not to module:make-* generators.