DEV Community

Cover image for Laravel 7: Customize stubs
Zubair Mohsin
Zubair Mohsin

Posted on

7 1

Laravel 7: Customize stubs

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

After running this command, a new directory will be added in your project.

stubs-directory

Let's have a look at most commonly used php artisan make:controller command stub file controller.plain.stub

<?php

namespace {{ namespace }};

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

class {{ class }} extends Controller
{
    //
}
Enter fullscreen mode Exit fullscreen mode

You can make any kind of changes to this stub file.

Example

Let's say we want every controller to have an __invoke() function.

<?php

namespace {{ namespace }};

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

class {{ class }} extends Controller
{
    public function __invoke()
    {

    }
}
Enter fullscreen mode Exit fullscreen mode

Now let's make a controller:

php artisan make:controller StubsTestController

Output:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class StubsTestController extends Controller
{
    public function __invoke()
    {

    }
}
Enter fullscreen mode Exit fullscreen mode

Happy Stubbing 👨🏽‍💻☝🏽

Let me know in comments how this can help you 🤓

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (2)

Collapse
 
wmiriye profile image
Wes Miriye

I am a bit confused where everyone is getting the stub constants from. For example, something like $FIELD_BODY$ or $ROUTE_NAMED_PREFIX$.

Where is the documentation for stub variables? I am having trouble finding it.

Collapse
 
bertheyman profile image
Bert Heyman

Thanks for writing this article!

Quite nice that the stubs are available to customise.
Only thing I miss though would be a way of using more variables inside the stub. A table name for a model for example, could perhaps be guessed like this: Str::plural(Str::snake($this->argument('model'))).

But extra variables would make things too complex to keep compatible with the generating functions, so I guess for these use cases a custom generator function would be a better choice.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs