Yii2 console控制台

Yii2 console控制台
在console 地下创建控制器
在这里插入图片描述

<?php 
namespace console\controllers;

use yii\console\Controller;
use yii\console\ExitCode ;
use yii\helpers\Console;


class HelloController extends Controller
{
    public $message;
    
    public function options($actionID)
    {
        return ['message'];
    }
    
    public function optionAliases()
    {
        return ['m' => 'message']; //别名
    }
    
    public function actionIndex()
    {
       echo $this->message;
    }
     // 命令 "yii example/create test" 会调用 "actionCreate('test')"
    public function actionCreate($name) {   }

    // 命令 "yii example/index city" 会调用 "actionIndex('city', 'name')"
    // 命令 "yii example/index city id" 会调用 "actionIndex('city', 'id')"
     

    // 命令 "yii example/add test" 会调用 "actionAdd(['test'])"
    // 命令 "yii example/add test1,test2" 会调用 "actionAdd(['test1', 'test2'])"
    public function actionAdd(array $name) {    }
}
 ?>

在这里插入图片描述
有趣的要来了控制输出的字体颜色
引入use yii\helpers\Console;
Console::output("HELLO"); 输出 hello
Console::ansiFormat("要输出的内容",[颜色....]);

字体颜色有
FG_BLACK
FG_RED
FG_GREEN
FG_YELLOW
FG_BLUE
FG_PURPLE
FG_CYAN
FG_GREY
背景颜色
BG_BLACK
BG_RED
BG_GREEN
BG_YELLOW
BG_BLUE
BG_PURPLE
BG_CYAN
BG_GREY
如何输出一个 字体蓝色 背景黄色

$test = Console::ansiFormat("Hello",[Console::FG_BLUE,Console::BG_YELLOW]); 
Console::output("{$test}");

在这里插入图片描述
获取用户输入的

$test = Console::input("请输入- : ");
 Console::output("{$test}");

在这里插入图片描述
判断用户输入 yes 还是no

$test = Console::confirm("yes or  no ? ");
Console::output("{$test}");

在这里插入图片描述
让用户选择

        $test = Console::select("select  ? ",['1'=>'跑步','2'=>'游泳','3'=>'滑板']);
        Console::output("{$test}");

在这里插入图片描述
进度条

    Console::startProgress(0, 100,[Console::FG_BLACK]);
    for ($n = 1; $n <= 100; $n++) {
        usleep(2000);
        Console::updateProgress($n, 100);
    }
    Console::endProgress();

在这里插入图片描述

对输入的数据验证

        $test = Console::prompt("Like for Phper",['required'=>true,'error'=>'必须输入']);
        Console::output("{$test}");
required 真假,是否必须填写
default 默认值
pattern 正则匹配
validator 自定义验证函数
error 错误信息 

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值