Add your own module commands and generators
Register a custom Artisan command in one of two places:
- The
$commandsproperty of a module provider, for commands owned by that module. See Add Artisan commands to a module. - The
commandskey inconfig/modules.php, for application-level commands and custom module generators.
Register a command from the application
Section titled “Register a command from the application”Publish config/modules.php if you have not already, then merge your command class into commands:
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag=config'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.
Write a generator
Section titled “Write a generator”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.
-
Add a generator path in
config/modules.php.config/modules.php 'generator' => [// ...'dto' => ['path' => 'app/DTOs', 'generate' => false],], -
Create the stub file.
stubs/nwidart-stubs/dto.stub <?phpnamespace $NAMESPACE$;final readonly class $CLASS${public function __construct() {}} -
Write the generator command. Extend
GeneratorCommand, useModuleCommandTraitto resolve the target module (from the argument ormodule:use), and set$argumentNameso the base class validates the name.app/Console/Commands/MakeDtoCommand.php <?phpnamespace 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();}} -
Register
\App\Console\Commands\MakeDtoCommand::classin thecommandslist ofconfig/modules.php, as shown above. -
Check the result.
Terminal window php artisan module:make-dto OrderData BlogThis creates
Modules/Blog/app/DTOs/OrderData.phpin theModules\Blog\DTOsnamespace.GeneratorCommandadds--force(-f) automatically to overwrite an existing file.