Documentation Index
Fetch the complete documentation index at: https://kawax.biz/llms.txt
Use this file to discover all available pages before exploring further.
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 isgetStub(). Everything else is optional, but in practice you will also set the following members.
| Member | Kind | Role |
|---|---|---|
$name | property | Command name, e.g. make:handler |
$description | property | Human-readable command description |
$type | property | Label used in the success message, e.g. Handler |
getStub() | method (required) | Return the path to the stub file |
getDefaultNamespace() | method | Control the default generation namespace |
make:handler command:
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 bygetStub(). 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
handler.stub
GeneratorCommand automatically replaces the following placeholders in the stub:
| Placeholder | Replaced with | Legacy alias |
|---|---|---|
{{ namespace }} | Fully qualified namespace of the generated class | DummyNamespace |
{{ class }} | Class name | DummyClass |
{{ rootNamespace }} | Application root namespace | DummyRootNamespace |
{{ 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 theresolveStubPath() pattern. First, publish the stubs from your service provider’s boot() method:
getStub() to prefer a user-provided stub when one exists:
php artisan vendor:publish --tag=stubs, users can edit stubs/handler.stub in their project root to customize the generated output.
Registering in a service provider
Register the command in your service provider’sboot() method. Wrap it in runningInConsole() to avoid loading it on every web request.
extra.laravel key to your composer.json so users do not need to register the provider manually:
Use cases
Custom form request generator
Custom form request generator
Generate request classes that extend your own base request, pre-wired with your authorization logic. Return
$rootNamespace.'\Http\Requests' from getDefaultNamespace().DTO generator
DTO generator
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.Action class generator
Action class generator
Generate single-responsibility Action classes at
App\Actions with an execute() method ready to implement.How Livewire uses the same approach
How Livewire uses the same approach
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.Related pages
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.