Build interactive menus
Interactive menus in console applications are very powerful. They
provide a simple interface that requires little interaction. With Laravel
Zero, you can use the
menu method to create beautiful menus:
Using menus in the console may sound silly, but is fantastic! Your users don't need to type the number corresponding to their choice any more. They can just use the arrows on their keyboard to make their selection!
Create your first menu by copy pasting the code below in your commands
$option = $this->menu('Pizza menu', [ 'Freshly baked muffins', 'Freshly baked croissants', 'Turnovers, crumb cake, cinnamon buns, scones', ])->open(); $this->info("You have chosen the option number #$option");
When you now run your command your output should be similar to this image:
Changing the appearance
The appearance of the menu can be set with a fluent API. What if we like a green font on a black background? The code below shows you how to do just that and some extras.
$this->menu($title, $options) ->setForegroundColour('green') ->setBackgroundColour('black') ->setWidth(200) ->setPadding(10) ->setMargin(5) ->setExitButtonText("Abort") // remove exit button with // ->disableDefaultItems() ->setUnselectedMarker('❅') ->setSelectedMarker('✏') ->setTitleSeparator('*-') ->addLineBreak('<3', 2) ->addStaticItem('AREA 2') ->open();
Behind the scenes, the
menumethod uses the
nunomaduro/laravel-console-menupackage. You can find more details on how to use the menu method there.