Command line applications.
A demo of how Laravel Prompts can help you create lovely command line interface (CLI) apps.
Laravel is not only good for web applications but also command line (CLI) applications, these apps benefit from all the good things in Laravel like Collections and Eloquent.
These commands are run via the artisan
tool which means you can run your CLI
apps manually or on a schedule, as we are focusing on Laravel Prompts here, you will most likely just
run them manually.
> php artisan app:prompts-demo This demo application will show off some of the Laravel Prompts features. ┌ What is your name? ──────────────────────────────────────────┐ │ Sam │ └──────────────────────────────────────────────────────────────┘ Hello Sam, nice to meet you. ┌ Please enter your password ──────────────────────────────────┐ │ ••••••••••••• │ └──────────────────────────────────────────────────────────────┘ ┌ What day is it today? ───────────────────────────────────────┐ │ › ● Monday ┃ │ │ ○ Tuesday │ │ │ ○ Wednesday │ │ │ ○ Thursday │ │ │ ○ Friday │ │ └──────────────────────────────────────────────────────────────┘ Thanks, please wait while I count to 10. ┌ Counting in progress, please wait... ────────────────────────┐ │ ████████████████████████████████████████████████████████████ │ └─────────────────────────────────────────────────────── 10/10 ┘ Thanks for waiting.
The command above is a simple application which shows off some of the data collection and feedback modules provided by Laravel Prompts, they look better in the actual console, but the above should give you an idea.
A quick walkthrough of how this works.
My assumptions, at the very least, are that you have Laravel setup and working.
To install Prompts, just run the following composer command.
composer require laravel/prompts
That's all you need to do, you can now start writing your CLI PHP applications, check the documentation linked below for available components.
To create the starter file for our command, we can run the following command:
php artisan make:command PromptsDemo
This will create the file app/Console/Commands/PromptsDemo.php
, here you can
define the logic for your command, in this case it looks like this:
1<?php 2 3namespace App\Console\Commands; 4 5use Illuminate\Console\Command; 6use function Laravel\Prompts\password; 7use function Laravel\Prompts\progress; 8use function Laravel\Prompts\select; 9use function Laravel\Prompts\spin;10use function Laravel\Prompts\text;11use function Laravel\Prompts\info;12 13class PromptsDemo extends Command14{15 /**16 * The name and signature of the console command.17 *18 * @var string19 */20 protected $signature = 'app:prompts-demo';21 22 /**23 * The console command description.24 *25 * @var string26 */27 protected $description = 'A demo command to show off features of Laravel Prompts';28 29 /**30 * Execute the console command.31 */32 public function handle()33 {34 info('This demo application will show off some of the Laravel Prompts features.');35 36 $name = text('What is your name?');37 info('Hello ' . $name . ', nice to meet you.');38 39 $password = password('Please enter your password', required: true);40 spin(41 callback: fn () => sleep(4),42 message: 'Processing...'43 );44 45 $day = select(46 label: 'What day is it today?',47 options: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],48 );49 50 info('Thanks, please wait while I count to 10.');51 52 $numbers = range(1,10);53 progress(54 label: 'Counting in progress, please wait...',55 steps: $numbers,56 callback: fn () => sleep(1),57 );58 59 info('Thanks for waiting.');60 }61}
Commands in Laravel are run via the artisan
tool, so, to run our command:
php artisan app:prompts-demo
A few links to supporting resources.