Overview: Elevating the Command Line Experience For years, PHP developers have relied on the Symfony Console component to handle command-line interactions. While functional, these traditional prompts often feel static and lack the polish found in modern JavaScript ecosystems. Laravel Prompts, introduced by Jess Archer, changes this by providing a suite of functions designed to create beautiful, user-friendly CLI inputs. This package isn't just about aesthetics; it introduces critical features like live validation, searchable selects, and multi-select capabilities that were previously cumbersome to implement in PHP. By focusing on the developer experience (DX), Laravel now offers a command-line interface that feels as responsive and intuitive as a modern web form. This matters because well-designed CLI tools reduce user error and lower the barrier to entry for complex administrative tasks. Prerequisites To follow this tutorial, you should have a solid grasp of the following: * **PHP 8.x**: The package utilizes modern PHP features like named parameters and arrow functions. * **Laravel Artisan**: Familiarity with creating and running console commands via `php artisan`. * **Composer**: Knowledge of how to manage PHP dependencies. Key Libraries & Tools * **Laravel Prompts**: The core package providing the interactive functional API. * **Artisan**: Laravel's built-in command-line interface. * **ANSI Escape Sequences**: The underlying technology used to manipulate the terminal cursor and colors. Code Walkthrough: Implementing Core Features Unlike the class-based approach in Symfony, Laravel Prompts uses namespaced functions. This keeps your code clean and expressive. Text Input and Validation The `text` function replaces the old `$this->ask()` method. It supports placeholders and sophisticated validation logic. ```python use function laravel\prompts\text; $name = text( label: 'What is your name?', placeholder: 'E.g. Taylor Otwell', required: true, validate: fn (string $value) => match (true) { strlen($value) < 3 => 'The name must be at least 3 characters.', default => null } ); ``` In this snippet, we use **named parameters** to define the prompt. The `validate` callback allows for real-time feedback. Once a user hits enter and triggers an error, the prompt enters a **live validation mode**, updating the error message as the user types. Interactive Selects and Search Selecting from a list is a common requirement. The `select` and `search` functions offer a significant upgrade over numeric-based choices. ```python use function laravel\prompts\select; use function laravel\prompts\search; // Basic Select $role = select( label: 'What role should the user have?', options: [ 'admin' => 'Administrator', 'editor' => 'Editor', 'viewer' => 'Viewer', ] ); // Eloquent-powered Search $id = search( label: 'Search for a user to email', options: fn (string $value) => strlen($value) > 0 ? User::where('name', 'like', "%{$value}%")->pluck('name', 'id')->all() : [] ); ``` The `search` function is particularly powerful. It accepts a callback that can query a database in real-time as the user types, returning an associative array where the key is the value returned to your code and the value is the label shown to the user. Syntax Notes: Named Parameters and Traits Laravel Prompts leans heavily into **PHP 8's named parameters**. Because these functions often have many optional arguments (like `default`, `placeholder`, and `hint`), named parameters allow you to skip the ones you don't need without passing multiple `null` values. When integrating these into existing Laravel commands, you should implement the `PromptsForMissingInput` interface. This allows Artisan to automatically trigger prompts if a required argument is missing, rather than simply throwing a "Not enough arguments" error. This creates a seamless fallback for users who forget the specific syntax of your command. Practical Examples: Artisan Integration Beyond simple scripts, Laravel Prompts is now deeply integrated into the Laravel framework itself. For instance, when running `php artisan make:model`, if you omit the name, the framework uses a text prompt with a placeholder suggesting conventions like "Flight". Another excellent use case is the `vendor:publish` command. Previously, this generated a massive, hard-to-read list. Now, it uses the `select` function with a defined **scroll height**, allowing users to navigate hundreds of options using arrow keys or even `hjkl` for Vim enthusiasts. This demonstrates the package's ability to handle scale while maintaining a clean UI. Tips & Gotchas * **Windows Compatibility**: Laravel Prompts relies on specific terminal features. If you are on Windows and not using **WSL (Windows Subsystem for Linux)**, the package will automatically fall back to standard Symfony prompts. Always test your commands in the environment your team uses. * **Cancelation Handling**: When a user hits `Ctrl+C`, the prompt enters a "canceled" state, often striking through the input. Ensure your logic accounts for cases where a user might abort the process mid-stream. * **Live Validation Timing**: Validation only starts after the user first attempts to submit (hits Enter). This prevents annoying "field is required" errors while the user is still in the middle of typing their initial response. * **Non-Laravel Projects**: You can use this package in any PHP project. Simply run `composer require laravel/prompts` to bring these interactive features to any CLI tool.
Symfony
Companies
- Jul 27, 2023
- Feb 9, 2022