What Is Stub Customization Commands In Laravel Framework

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

The Artisan console's make commands are used to create a variety of classes, such as controllers, jobs, migrations, and tests.

What Is Stub Customization Commands In Laravel Framework

Stub Customization

These classes are generated using "stub" files that are populated with values based on your input. However, you may sometimes wish to make small changes to files generated by Artisan. To accomplish this, you may use the stub:publish command to publish the most common stubs for customization:
 

In Laravel 7, you will be able to customize stubs.

Publishing stubs

In order to customize stub files, you need to publish them:

 

php artisan stub:publish


The published stubs will be located within a stubs directory in the root of your application. Any changes you make to these stubs will be reflected when you generate their corresponding classes using Artisan make commands.

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 occurences 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/demoController --api


we will see the result of our customized controller in app/Http/Controllers/API/demoController.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 demo


the model demo will contain the $fillable property.

Related Post