How to use Laravel commands to create files from stubs

Guilherme Albert
3 min readMar 16, 2023

--

Laravel is a powerful PHP framework that provides developers with a rich set of tools to build web applications. One of these tools is the Artisan command-line interface, which allows developers to perform various tasks quickly and easily. One of the most useful features of Artisan is its ability to create files from stubs.

Laravel: The PHP Framework

In this article, we will discuss what stubs are and how to use Laravel commands to create files from them.

What are Stubs?

In the context of Laravel, a stub is a pre-written file that contains placeholders for dynamic content. These placeholders are replaced with actual values when a new file is created using the stub. Stubs are commonly used for generating repetitive files, such as model classes, migration files, and controller classes.

Laravel provides a set of default stubs for common file types, such as controllers, models, and migrations. However, you can also create your own stubs and use them to generate custom files.

Using Laravel Commands to Create Files from Stubs

To create a new file from a stub using Laravel, you need to use the make command with the appropriate arguments. The basic syntax for creating a file from a stub is as follows:

php artisan make:<file-type> <file-name>

In this command, <file-type> refers to the type of file you want to create, such as a controller or a model. <file-name> refers to the name you want to give to the new file.

For example, to create a new controller file named HomeController, you would use the following command:

php artisan make:controller HomeController

This command will create a new file called HomeController.php in the app/Http/Controllers directory. The contents of the file will be based on the default controller stub provided by Laravel.

You can also use custom stubs to create new files.

Customizing stubs If you want to customize the stub used by a particular command, you can use the “ — stub” option followed by the path to your custom stub file. For example, to create a new controller using a custom stub file located in the “stubs” directory, you can use the following command:

php artisan make:controller MyController --stub=stubs/controller.stub

This command will create a new file called “MyController.php” in the “app/Http/Controllers” directory, using the custom stub file located in the “stubs” directory.

Creating custom stubs If you want to create your own custom stubs, you can do so by creating a new file in the “resources/stubs” directory with the appropriate file extension. For example, to create a new custom controller stub, you can create a file called “controller.stub” in the “resources/stubs” directory.

Inside the stub file, you can use placeholder variables such as “{{namespace}}” and “{{class}}” to generate dynamic content based on the file name and location. For example, here is a simple controller stub that uses placeholder variables:

<?php

namespace {{namespace}};

use Illuminate\Http\Request;

class {{class}} extends Controller
{
//
}

This stub file will create a new controller class with the same name as the file, and with the appropriate namespace and inheritance.

Laravel’s CLI and stub system make it easy to create new files with a consistent structure and formatting, saving developers time and effort.

By using the “make” command and customizing or creating your own stubs, you can quickly create new files for your Laravel application with minimal effort.

--

--

Guilherme Albert

Lorem ipsum dolor sit amet, consectetur adipiscing elit.