Skip to main content

What is GeneratorCommand?

Illuminate\Console\GeneratorCommand is the abstract base class behind every code-generation command built into Laravel. make:model, make:controller, make:request, and the rest all extend it. By providing your own make:xxx command, users can run php artisan make:handler OrderHandler and get a correctly namespaced file instead of creating the boilerplate by hand.
GeneratorCommand is barely mentioned in the official documentation. Understanding it requires reading the framework source code directly — a classic advanced topic.

Minimal implementation

The only method you must implement is getStub(). Everything else is optional, but in practice you will also set the following members. Here is a complete make:handler command:
With getDefaultNamespace() returning $rootNamespace.'\Handlers', running php artisan make:handler OrderHandler generates App\Handlers\OrderHandler at app/Handlers/OrderHandler.php.

Creating the stub file

Place the stub file at the path returned by getStub(). A stub is a PHP template whose placeholders are replaced with the resolved namespace and class name.
src
Console
Commands
HandlerMakeCommand.php
stubs
handler.stub
Example stub file:
handler.stub
GeneratorCommand automatically replaces the following placeholders in the stub: Both {{ namespace }} and DummyNamespace produce the same result. Use the {{ }} style for new stubs — it matches current Laravel conventions.

Allowing stub customization

To let users override your stubs, use the resolveStubPath() pattern. First, publish the stubs from your service provider’s boot() method:
Then update getStub() to prefer a user-provided stub when one exists:
After running php artisan vendor:publish --tag=stubs, users can edit stubs/handler.stub in their project root to customize the generated output.
The resolveStubPath() pattern is the same one used in Laravel’s built-in RequestMakeCommand. Follow it so your package behaves consistently with the framework.

Registering in a service provider

Register the command in your service provider’s boot() method. Wrap it in runningInConsole() to avoid loading it on every web request.
Add the extra.laravel key to your composer.json so users do not need to register the provider manually:

Use cases

Generate request classes that extend your own base request, pre-wired with your authorization logic. Return $rootNamespace.'\Http\Requests' from getDefaultNamespace().
Generate Data Transfer Objects with readonly properties and a from() factory method. Put all the boilerplate in the stub so developers only fill in the data fields.
Generate single-responsibility Action classes at App\Actions with an execute() method ready to implement.
Livewire’s make:livewire command extends GeneratorCommand and overrides handle() to generate two files at once — the component class and the Blade view. Study it when you need to generate multiple files from a single command.

Testing

Use Orchestra Testbench to assert that your command generates the expected file with the correct content.
Generator commands write to the real filesystem. Use tearDown() for cleanup so it runs even when a test fails midway.

Laravel package development

Review the service provider foundations before adding commands to your package.

Testing Laravel packages with Orchestra Testbench

Learn how to set up the Testbench foundation for package tests.
Last modified on July 13, 2026