PHPackages                             vxm/yii2-console-menu - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. vxm/yii2-console-menu

ActiveYii2-extension[CLI &amp; Console](/categories/cli)

vxm/yii2-console-menu
=====================

Pretty console menu for Yii2

1.0.0(7y ago)921BSD-3-ClausePHPPHP &gt;=7.1

Since Apr 10Pushed 7y ago1 watchersCompare

[ Source](https://github.com/vuongxuongminh/yii2-console-menu)[ Packagist](https://packagist.org/packages/vxm/yii2-console-menu)[ RSS](/packages/vxm-yii2-console-menu/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Yii2 Console Menu
=================

[](#yii2-console-menu)

[![Latest Stable Version](https://camo.githubusercontent.com/ebd75bfa9e7c8887e78be06980e1109829d2b88c51e95f7d2e192255d58e046e/68747470733a2f2f706f7365722e707567782e6f72672f76786d2f796969322d636f6e736f6c652d6d656e752f762f737461626c65)](https://packagist.org/packages/vxm/yii2-console-menu)[![Total Downloads](https://camo.githubusercontent.com/82710a12860c9d9ee4c6ec56b8d609fae4c32244c3947430a75ecf6bc0a1e83b/68747470733a2f2f706f7365722e707567782e6f72672f76786d2f796969322d636f6e736f6c652d6d656e752f646f776e6c6f616473)](https://packagist.org/packages/vxm/yii2-console-menu)[![Build Status](https://camo.githubusercontent.com/b34cb54386aae896cf5243081e3cf1b3b55e877d88be50d8c10fc98a0a473bfc/68747470733a2f2f7472617669732d63692e6f72672f76756f6e6778756f6e676d696e682f796969322d636f6e736f6c652d6d656e752e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/vuongxuongminh/yii2-console-menu)[![Code Coverage](https://camo.githubusercontent.com/0b933e67b0be0fa831be5db2d2f956b92438ec5827fe84a9fefed2d44580d37f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f76756f6e6778756f6e676d696e682f796969322d636f6e736f6c652d6d656e752f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/vuongxuongminh/yii2-console-menu/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/41029836eaaff707c12f2e6600761a61d5891731d750ec3fe2d202471c43b4eb/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f76756f6e6778756f6e676d696e682f796969322d636f6e736f6c652d6d656e752f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/vuongxuongminh/yii2-console-menu/?branch=master)[![Yii2](https://camo.githubusercontent.com/d6b0929173e28cc627430d2519ca1853466a70f37395877eaf4820cb3e1e1909/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f77657265645f62792d5969695f4672616d65776f726b2d677265656e2e7376673f7374796c653d666c6174)](http://www.yiiframework.com/)

About it
--------

[](#about-it)

An extension support build pretty menu base on [nunomaduro/laravel-console-menu](https://github.com/nunomaduro/laravel-console-menu), and is a [php-school/cli-menu](https://github.com/php-school/cli-menu) wrapper for Yii2 console controller.

Requirements
------------

[](#requirements)

- [PHP &gt;= 7.1](http://php.net)
- [yiisoft/yii2 &gt;= 2.0.13](https://github.com/yiisoft/yii2)

Installation
------------

[](#installation)

Require Yii2 console menu using [Composer](https://getcomposer.org):

```
composer require vxm/yii2-console-menu
```

Usage
-----

[](#usage)

### Quick Setup

[](#quick-setup)

 [![](resource/demo_0.png)](resource/demo_0.png)

```
use yii\console\Controller;

/**
 * @method \vxm\consoleMenu\Menu menu(string $title = '', array $options = [])
*/
class TestController extends Controller
{
    /**
     * Execute the action.
     *
     * @return void
     */
    public function actionTest()
    {
        $option = $this->menu('Pizza menu', [
            'Freshly baked muffins',
            'Freshly baked croissants',
            'Turnovers, crumb cake, cinnamon buns, scones',
        ])->open();

        $this->stdout("You have chosen the option number #$option");
    }
}
```

### Setup with a question

[](#setup-with-a-question)

 [![](resource/demo_1.png)](resource/demo_1.png)

```
use yii\console\Controller;

/**
 * @method \vxm\consoleMenu\Menu menu(string $title = '', array $options = [])
*/
class TestController extends Controller
{
    /**
     * Execute the action.
     *
     * @return void
     */
    public function actionTest()
    {
        $option = $this->menu('Pizza menu')
                    ->addOption('mozzarella', 'Mozzarella')
                    ->addOption('chicken_parm', 'Chicken Parm')
                    ->addOption('sausage', 'Sausage')
                    ->addQuestion('Make your own', 'Describe your pizza...')
                    ->addOption('burger', 'Prefer burgers')
                    ->setWidth(80)
                    ->open();

        $this->stdout("You have chosen the text option: $option");
    }
}
```

### Setup with advanced option, in this case, a password

[](#setup-with-advanced-option-in-this-case-a-password)

 [![](resource/demo_2.png)](resource/demo_2.png)

```
use yii\console\Controller;

/**
 * @method \vxm\consoleMenu\Menu menu(string $title = '', array $options = [])
*/
class TestController extends Controller
{
    /**
     * Execute the action.
     *
     * @return void
     */
    public function actionTest()
    {
        $menu = $this->menu('Pizza menu')
                    ->addOption('mozzarella', 'Mozzarella')
                    ->addOption('chicken_parm', 'Chicken Parm')
                    ->addOption('sausage', 'Sausage')
                    ->addQuestion('Make your own', 'Describe your pizza...');

        $itemCallable = function (\PhpSchool\CliMenu\CliMenu $cliMenu) use ($menu) {
            $cliMenu->askPassword()
                ->setValidator(function ($password) {
                    return $password === 'secret';
                })
                ->setPromptText('Secret password?')
                ->ask();

            $menu->setResult('Free spice!');

            $cliMenu->close();
        };
        $menu->addItem('Add extra spice for free (password needed)', $itemCallable);

        $option = $menu->addOption('burger', 'Prefer burgers')
            ->setWidth(80)
            ->open();

        $this->stdout("You have chosen the text option: $option");
    }
}
```

### Appearance

[](#appearance)

Available colors: `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white`.

```
  $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('
