Laravel 8 Stub Customization

Share this Article
Reading Time: 5 minutes
8,654 Views

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 are the core template files which laravel uses to generate classes skeletons.

Laravel stub feature is included from laravel 7 version. Inside this article we will see Laravel 8 stub customization. We can generate Stub and publish it inside application.

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.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your 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.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5