Unlock the code generation of Laravel (part 2): Stubs with Laravel Promts 💻🚀

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyrinNew
    Senior Member
    • Feb 2024
    • 5175

    #1

    Unlock the code generation of Laravel (part 2): Stubs with Laravel Promts 💻🚀

    As developers, we’re always on the lookout for tools and techniques that make our work more efficient, customizable, and user-friendly. If you’re working with Laravel, you’re in luck! Today, I’ll show you how to combine Laravel stubs with the Laravel Prompts package to create a fully customizable and interactive code generation workflow.


    By the end of this guide, you’ll be able to generate code templates dynamically based on user input, all while maintaining the power and flexibility of Laravel’s built-in tools. Ready to supercharge your development process? Let’s dive in! 💪


    What Are Laravel Stubs?

    Laravel stubs are template files used to generate boilerplate code in Laravel projects. Think of them as skeleton code that Laravel uses for commands like php artisan make:controller or php artisan make:model. You can customize these stubs to fit your project's needs, ensuring that your generated code matches your architecture and coding standards.


    For example, by modifying the default controller.stub, you can automatically include traits, middleware, or other boilerplate code every time you generate a new controller.


    How to Use and Customize Laravel Stubs

    1. Locate and Publish Stubs:


    Laravel keeps its default stubs hidden in the vendor directory. You’ll need to publish them into your project if you want to customize them:






    php artisan stubublish







    This will generate a stubs directory in the root of your project where you can modify the available stubs, like controller.stub or model.stub.


    2. Customize the Stub:


    For example, let’s add a custom trait to the controller stub:






    // controller.stub example
    namespace {{ namespace }};

    use App\Traits\YourCustomTrait;
    use Illuminate\Http\Request;

    class {{ class }} extends Controller
    {
    use YourCustomTrait;

    // Custom methods
    }







    Now, every time you generate a new controller, it will include the YourCustomTrait by default.


    To see more about stubs see part 1:


    Unlock the Magic of Laravel Stubs: Supercharge Your Code Generation 🚀

    Hello, Laravel enthusiasts! 🖐️ Welcome to the magical world of Laravel stubs — a powerful tool that can save you…

    medium.com


    What Is Laravel Prompts?

    Laravel Prompts is an excellent package that adds interactive CLI prompts to your Laravel commands. Whether you need to gather user input or make your commands more dynamic, Laravel Prompts makes it easy.


    Here’s how you can add interactive prompts to your Laravel commands using the package.


    Installing Laravel Prompts

    The latest versions of Laravel already include the Prompts package, so no need to install it separately. However, if you’re using a standalone PHP project, you can install it via Composer:






    composer require laravel/prompts







    More in https://laravel.com/docs/11.x/prompts


    Combining Laravel Stubs and Prompts for Ultimate Flexibility

    Let’s combine Laravel stubs with Laravel Prompts to create an Artisan command that dynamically generates a controller based on user input!


    Step 1: Create a Custom Artisan Command

    First, we’ll generate a new Artisan command using Laravel’s make:command command:






    php artisan make:command GenerateCustomController







    This will create a new command file in app/Console/Commands/GenerateCustomController.php. Now, let's edit this command to use Laravel Prompts and interact with our stubs.


    Step 2: Add Interactive Prompts to the Command

    In our command, we’ll use the Laravel Prompts package to gather user input. For example, we’ll ask the user for the name of the controller, whether it should be a resource controller, and if it should include any specific traits.


    Here’s the updated command:






Working...