nafeeur10's avatar

What is "stub" actually?

Laravel introduced stub customization in their documentation https://laravel.com/docs/8.x/artisan#stub-customization

But I want to know what is stub actually and why I need it? I saw

https://stackoverflow.com/questions/9777822/what-does-to-stub-mean-in-programming https://stackoverflow.com/questions/463278/what-is-a-stub

these two articles but can't understand!!!

Before https://laravel.com/docs/8.x/queues#class-structure

Job stubs may be customized using stub publishing
0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

A stub is a template for a class.

Laravel has stub file for many classes, e.g. this is a Controller class stub which should be familiar to you; it has all of the methods stubbed out along with bracketed placeholders for dynamic information (such as the namespace and class name).

Whenever you use the php artisan make:controller command, Laravel will fetch this text file and replace the placeholders with relevant information before creating the new Controller class in the app/Http/Controllers directory using the updated text from the stub for the class file contents:

<?php

namespace {{ namespace }};

use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;

class {{ class }} extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

vendor/laravel/framework/src/Illuminate/Routing/Console/stubs/controller.model.stub

Publishing stubs allows us to modify this template according to our preference; e.g. removal of docblocks, or removal of certain non-API methods like edit and create

6 likes

Please or to participate in this conversation.