Laravel Console Wizard


January 29th, 2021

Laravel Console Wizard

Laravel Console WIzzard is a package for creating multi-step wizards with complex input inside the console. Its primary purpose is to collect data for file generators—here’s an example from the project’s readme:

1namespace App\Console\Commands;
3use Shomisha\LaravelConsoleWizard\Command\Wizard;
4use Shomisha\LaravelConsoleWizard\Steps\ChoiceStep;
5use Shomisha\LaravelConsoleWizard\Steps\TextStep;
7class IntroductionWizard extends Wizard
9 protected $signature = "wizard:introduction";
11 protected $description = 'Introduction wizard.';
13 public function getSteps(): array
14 {
15 return [
16 'name' => new TextStep("What's your name?"),
17 'age' => new TextStep("How old are you?"),
18 'gender' => new ChoiceStep("Your gender?", ["Male", "Female"]),
19 ];
20 }
22 public function completed()
23 {
24 $this->line(sprintf(
25 "This is %s and %s is %s years old.",
26 $this->answers->get('name'),
27 ($this->answers->get('gender') === 'Male') ? 'he' : 'she',
28 $this->answers->get('age')
29 ));
30 }

This package works by defining a set of steps and then once completed, you can process data for output, generate files, or perform whatever you need to with the data.

  • TextStep – expects a text input answer (i.e., name)
  • MultipleAnswerTextStep – similar to TextStep, except it takes multiple answers and returns them in an array
  • ChoiceStep – this allows a user to pick one answer from multiple choices
  • MultipleChoiceStep – similar to ChoiceStep, but allows the user to pick multiple choices and returns the values as an array
  • UniqueMultipleChoiceStep – just like MultipleChoiceStep, but does not allow a user to select a single choice more than once
  • ConfirmStep – the user confirms yes or no, and this step returns a Boolean.

Learn More

You can learn more about this package, get full installation instructions, and view the source code on GitHub. This package also has a wiki providing in-depth documentation on step types, validation, and using the package.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.