Laravel Console Wizard

Packages

January 29th, 2021

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:

namespace App\Console\Commands;

use Shomisha\LaravelConsoleWizard\Command\Wizard;
use Shomisha\LaravelConsoleWizard\Steps\ChoiceStep;
use Shomisha\LaravelConsoleWizard\Steps\TextStep;

class IntroductionWizard extends Wizard
{
    protected $signature = "wizard:introduction";

    protected $description = 'Introduction wizard.';

    public function getSteps(): array
    {
        return [
            'name'   => new TextStep("What's your name?"),
            'age'    => new TextStep("How old are you?"),
            'gender' => new ChoiceStep("Your gender?", ["Male", "Female"]),
        ];
    }

    public function completed()
    {
        $this->line(sprintf(
            "This is %s and %s is %s years old.",
            $this->answers->get('name'),
            ($this->answers->get('gender') === 'Male') ? 'he' : 'she',
            $this->answers->get('age')
        ));
    }
}

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.