How to Use Stub Customization in Laravel
Arie Visser • April 19, 2020
laravel phpLaravel 7 came with a new feature, stub customization.
Stub customization gives the ability to modify the stub files that are used when creating classes such as controllers and tests with the artisan make
command.
In this guide I will try to give an overview of the stubs that can be customized, and describe two use cases.
Let's see what happens when we execute the command to enable customization:
php artisan stub:publish
You will see the "Stubs published successfully." message, and a new folder stubs
becomes visible in the root of your project, containing files that are customizable.
The following list shows all the stubs, with the accompanying command:
controller.api.stub
php artisan make:controller {ControllerName} --api
controller.invokable.stub
php artisan make:controller {ControllerName} --invokable
controller.model.api.stub
php artisan make:controller {ControllerName} --model={ModelName} --api
controller.model.stub
php artisan make:controller {ControllerName} --model={ModelName}
controller.nested.api.stub
php artisan make:controller {ControllerName} --parent={ParentModelName} --model={ModelName} --api
controller.nested.stub
php artisan make:controller {ControllerName} --parent={ParentModelName} --model={ModelName}
controller.plain.stub
php artisan make:controller {ControllerName}
controller.stub
php artisan make:controller {ControllerName} --resource
job.queued.stub
php artisan make:job {JobName}
job.stub
php artisan make:job {JobName} --sync
migration.create.stub
php artisan make:migration {migration_name} --create={table_name}
migration.stub
php artisan make:migration {migration_name}
migration.update.stub
php artisan make:migration {migration_name} --table={table_name}
model.pivot.stub
php artisan make:model {ModelName} --pivot
model.stub
php artisan make:model {ModelName}
test.stub
php artisan make:test {TestName}
test.unit.stub
php artisan make:test {TestName} --unit
All these stubs contain the basic content that is used to generate the files that are created with the artisan make
command.
After publishing these stubs, you have the ability to edit their content.
For this guide, we will add type hinting to API controllers by default, and a default property to models.
Add type hinting to API controllers
Type hinting (in combination with clear function names and git blame) can render doc blocks obsolete in many cases.
We can apply this code style to our controllers, by adding return type declarations and function argument types to each method in stubs/controller.api.stub
.
After adding use Illuminate\Http\Response;
, the int
type hint to the occurrences of $id
, and Response
as return type, the file will look like this:
<?php
namespace {{ namespace }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class {{ class }} extends Controller
{
public function index(): Response
{
//
}
public function store(Request $request): Response
{
//
}
public function show(int $id): Response
{
//
}
public function update(Request $request, int $id): Response
{
//
}
public function destroy(int $id): Response
{
//
}
}
Now, after we run:
php artisan make:controller API/OrderController --api
we will see the result of our customized controller in app/Http/Controllers/API/OrderController.php
, containing the type hints.
Add a default property to models
In most models I create, I like to define the attributes that are mass assignable, by adding the $fillable
property to the model.
Let's add this by default by customizing model.stub
:
<?php
namespace {{ namespace }};
use Illuminate\Database\Eloquent\Model;
class {{ class }} extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
//
];
}
When we create a model, by running:
php artisan make:model Order
the model Order
will contain the $fillable
property.
That's it. I hope this guide was helpful. You can find the code of these examples in this repository.