Tutorial : What is Laravel Stubs? How to use it?

Kaushik Thakkar
3 min readJul 1, 2023

Laravel, a popular PHP framework, offers developers a wide range of tools and features to streamline their application development process. One such feature is Laravel Stubs. In this article, we’ll explore what Laravel Stubs are, why they are important, and how to effectively use them in your projects. Whether you’re an experienced Laravel developer or just starting out, understanding Stubs can greatly enhance your development workflow.

What are Laravel Stubs?

In Laravel, Stubs are pre-defined template files that act as a starting point for generating new files or classes within your application. They contain the basic structure and boilerplate code required for different components like controllers, models, migrations, tests, and more. Stubs provide a convenient way to save time and effort by eliminating the need to write repetitive code from scratch.

The Importance of Laravel Stubs:

  1. Time and Effort Savings: Stubs help you avoid writing repetitive code for commonly used components. Instead of starting from scratch, you can generate files or classes using Stubs as a foundation, allowing you to focus on implementing specific functionality. This significantly reduces development time and effort, especially when creating multiple files.
  2. Consistency and Best Practices: Laravel Stubs follow Laravel’s coding standards and best practices. By using Stubs, you ensure that the generated files adhere to these standards, promoting code consistency across your application. This consistency makes it easier for other developers to understand and collaborate on your codebase.
  3. Customizability: Stubs provide a starting point that you can customize according to your project’s requirements. You can modify the generated code to fit your specific needs, add additional methods or properties, and implement the desired functionality. This flexibility allows you to tailor the code to match your project’s unique requirements.

Implementing Laravel Stubs:

Let’s walk through a practical example of how to implement Laravel Stubs for a UserController:

  • Open your command-line interface and navigate to your Laravel project directory.
  • You can use Laravel’s artisan command-line tool to create a custom command for generating classes using Stubs. To create a ‘make:customclass’ command, follow these steps:
php artisan make:command MakeCustomClass

Open the generated MakeCustomClass.php file located in the app/Console/Commands directory.

  • In the handle method of the class, you can define the logic to generate your desired class using a Stub. For example, you can use the file_get_contents and file_put_contents functions to read the Stub file, replace placeholders with the desired class name, and save the generated class file. Don't forget to provide appropriate feedback to the user after the class is generated.
  • Once you have defined your custom command, you can run it in the command line as follows:
php artisan make:customclass MyCustomClass

This will generate a new MyCustomClass.php file based on the defined Stub, inside a directory like app/CustomClasses or any other directory structure you prefer.

Creating Custom Stubs: While Laravel provides default Stubs, you can also create your own custom Stubs or modify the existing ones to match your coding standards or project requirements. To create a custom Stub, follow these steps:

  1. Navigate to or create the stubs directory within your Laravel root directory.
  2. Create a new Stub file or modify an existing one based on your requirements. Make sure the Stub file contains the necessary code structure and placeholders you want to generate. Here is example to create a Service Class
<?php

namespace $NAMESPACE$;

class $CLASS_NAME$Service
{

}
<?php

namespace $NAMESPACE$;

class $CLASS_NAME$Service
{

}

--

--

Kaushik Thakkar

Experienced SE with 7 years crafting innovative tech solutions. I'm Kaushik, a creative individual seeking new challenges.