Table of Contents
When we use Laravel artisan command to generate controller, model, factory, migration etc then we can see we get a code skeleton of each associated class or file. That well written code skeleton called as Laravel stub.
Laravel stub feature is included from laravel 7 version. Inside this article we will see Laravel 8 stub customization. If suppose we want to by default few methods, variables into class file of controller, model etc then we can generate.
When we write any code in stub files then it will come with generated file. For example, if we add few methods in controller stub file, then whenever we generate controller file from php artisan command. Those added methods by default will come with file.
Let’s see all discussion in action.
Installation of Laravel 8 Application
Laravel Installation can be done in two ways.
- Laravel Installer
- By using composer
Laravel Installer
To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.
$ composer global require laravel/installer
This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –
$ laravel
This command will open a command palette of Laravel Installer.
To create ad install laravel project in system,
$ laravel new blog
With the name of blog a laravel project will be created at your specified path.
By using composer
Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Click here to Install Composer ? Here is the complete command to create a laravel project-
$ composer create-project --prefer-dist laravel/laravel blog
After following these steps we can install a Laravel 8 application into system. To start the development server of Laravel –
$ php artisan serve
This command outputs –
Starting Laravel development server: http://127.0.0.1:8000
Assuming laravel 8 already installed at system.
What are Laravel Stub ?
The Artisan console’s make
commands are used to create a variety of classes, such as controllers, jobs, migrations, and tests. These classes are generated using “stub”.
These files provide a predefined skeleton for controller, model etc.
Example:
<?php namespace {{ namespace }}; use {{ rootNamespace }}Http\Controllers\Controller; use Illuminate\Http\Request; class {{ class }} extends Controller { // }
When we run the command $ php artisan make:controller TestController Given stub file will execute, replace all placeholders in a dynamic way and generate controller.
Laravel Stub Publish
To work with laravel stub files, first we need to publish it. When we publish stub then all files of stub will be located within a stubs
directory in the root of your application.
$ php artisan stub:publish

Laravel Stub Files
Above image indicated the stubs folder at project root. When we open, we can see we have several files which is basically a skeleton file for migration, controller, model etc.

Examples:
- controller.stub -> To generate resource controller $ php artisan make:controller ProductController –resource
- controller.plain.stub -> To generate plain controller without any methods $ php artisan make:controller ProductController
- factory.stub -> To generate a factory file $ php artisan make:factory ProductFactory
Laravel Stub File Customization
Let’s open controller.plain.stub from stubs directory.
<?php namespace {{ namespace }}; use {{ rootNamespace }}Http\Controllers\Controller; use Illuminate\Http\Request; class {{ class }} extends Controller { // //.. first method public function method1(){ return 'First Method'; } //.. second method public function method2(){ return 'Second Method'; } }
Above is the updated code we have written inside plain controller stub file. Next, whenever we generate controller let’s say
$ php artisan make:controller ProductController
Following command will generate a controller file at /app/Http/Controllers/ProductController.php
Open ProductController.php File,
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class ProductController extends Controller { // //.. first method public function method1(){ return 'First Method'; } //.. second method public function method2(){ return 'Second Method'; } }
Now, we are getting 2 default methods added into controller file.
This laravel stub customization is useful in those cases when we want some global methods or variables to each generated files of laravel application.
We hope this article helped you to learn about i.e Laravel 8 Stub Customization Tutorial in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.
Hi, I am Sanjay the founder of ONLINE WEB TUTOR. I welcome you all guys here to join us. Here you can find the web development blog articles. You can add more skills in web development courses here.
I am a Web Developer, Motivator, Author & Blogger. Total experience of 7+ years in web development. I also used to take online classes including tech seminars over web development courses. We also handle our premium clients and delivered up to 50+ projects.