Skip to content

Add your own module commands and generators

Register a custom Artisan command in one of two places:

  • The $commands property of a module provider, for commands owned by that module. See Add Artisan commands to a module.
  • The commands key in config/modules.php, for application-level commands and custom module generators.

Publish config/modules.php if you have not already, then merge your command class into commands:

Terminal window
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag=config
config/modules.php
'commands' => ConsoleServiceProvider::defaultCommands()
->merge([
\App\Console\Commands\SyncCatalogCommand::class,
])->toArray(),

Create the class before adding it to commands; a missing class stops every artisan command with a class-not-found error. Keep ConsoleServiceProvider::defaultCommands() so the built-in package commands stay registered.

This example adds module:make-dto, which generates a DTO class inside any module. Prerequisite: publish and enable stubs using the first three steps of Customise the generator stubs.

  1. Add a generator path in config/modules.php.

    config/modules.php
    'generator' => [
    // ...
    'dto' => ['path' => 'app/DTOs', 'generate' => false],
    ],
  2. Create the stub file.

    stubs/nwidart-stubs/dto.stub
    <?php
    namespace $NAMESPACE$;
    final readonly class $CLASS$
    {
    public function __construct() {}
    }
  3. Write the generator command. Extend GeneratorCommand, use ModuleCommandTrait to resolve the target module (from the argument or module:use), and set $argumentName so the base class validates the name.

    app/Console/Commands/MakeDtoCommand.php
    <?php
    namespace App\Console\Commands;
    use Illuminate\Support\Str;
    use Nwidart\Modules\Commands\Make\GeneratorCommand;
    use Nwidart\Modules\Support\Config\GenerateConfigReader;
    use Nwidart\Modules\Support\Stub;
    use Nwidart\Modules\Traits\ModuleCommandTrait;
    use Symfony\Component\Console\Input\InputArgument;
    class MakeDtoCommand extends GeneratorCommand
    {
    use ModuleCommandTrait;
    protected $name = 'module:make-dto';
    protected $description = 'Create a new DTO class for the specified module.';
    protected $argumentName = 'name';
    protected function getArguments(): array
    {
    return [
    ['name', InputArgument::REQUIRED, 'The name of the DTO class.'],
    ['module', InputArgument::OPTIONAL, 'The name of the module.'],
    ];
    }
    public function getDefaultNamespace(): string
    {
    return config('modules.paths.generator.dto.namespace')
    ?? $this->strip_app_folder(config('modules.paths.generator.dto.path', 'app/DTOs'));
    }
    protected function getDestinationFilePath(): string
    {
    $path = $this->moduleRepository()->getModulePath($this->getModuleName());
    $dtoPath = GenerateConfigReader::read('dto')->getPath() ?? config('modules.paths.app_folder').'DTOs';
    return $path.$dtoPath.'/'.Str::studly($this->argument('name')).'.php';
    }
    protected function getTemplateContents(): string
    {
    $module = $this->moduleRepository()->findOrFail($this->getModuleName());
    return Stub::create('/dto.stub', [
    'NAMESPACE' => $this->getClassNamespace($module),
    'CLASS' => $this->getClass(),
    ])->render();
    }
    }
  4. Register \App\Console\Commands\MakeDtoCommand::class in the commands list of config/modules.php, as shown above.

  5. Check the result.

    Terminal window
    php artisan module:make-dto OrderData Blog

    This creates Modules/Blog/app/DTOs/OrderData.php in the Modules\Blog\DTOs namespace. GeneratorCommand adds --force (-f) automatically to overwrite an existing file.