Laravel 8 Tutorial - Stub Customization

In this video, we will learn about Stub Customization in Laravel 8. The PHP artisan make commands are used to create a variety of classes, such as controllers, models, migrations, and jobs. 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, Laravel 8 provides the stub:publish command.

Let's run this stub publish command. Go to the command prompt and run the command:


php artisan stub:publish

Now, the stub is published. The published stubs will be located within a stubs directory. Go to the project and inside the project root directory, you can see the stub folder.

Just open this stubs folder and you can see all the stubs, such as the controller, job, migration, model, and test stubs. Let's open any one stub. So, let's open the controller.plain.stub file.

Inside this file, you can see that there are some default codes written here. Whenever we create any new controller, these default codes are written in that controller.

Now, I am going to create a new controller to show you. So, switch to the command prompt and type the command:


php artisan make:controller StudentController

Now, let's see the controller. Go to the project, click on the app folder, then HTTP, and go inside the controller folder. And just open the StudentController.

And you can see inside the StudentController that these are the codes which are coming from the controller.plain.stub file.

Alright, now make some changes inside the stub file. So, go to the controller.plain.stub file and just open it. And here, I am just going to create a function:


public function create()
{
//
}

Alright, now create another new controller and see the changes inside the controller. So, go to the command prompt and type:


php artisan make:controller StudentnewController

Ok, controller created. Now, switch to the project and here just open StudentnewController. You can see the differences. Here is the new default controller code written here ok.

So, in this way, we can customize any of these stubs.