1

如何要求输入并循环输入,直到用户使用 lumen 在命令行中输入?

app/Console/Commands默认情况下,我可以在目录中创建自定义命令

这是我当前的代码

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use \Exception;


class AskmeCommand extends Command
{


    protected  $signature = "askme";

    protected $description = "Ask Me";

    public function __construct() {
        parent::__construct();
    }

    public function handle()
    {
        $this->question("ask me question?");
        try {
            $this->info("Hello World");
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }
}

如果我通过php artisan askme它所做的一切运行此代码就是打印“问我问题”用户应该如何在命令行中输入输入/问题?那么如何再次循环问题?

4

1 回答 1

0

我认为它应该给你一个错误,因为我根本没有看到Command::question(),但是有ask()

https://laravel.com/docs/8.x/artisan#prompting-for-input

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    $name = $this->ask('What is your name?');
}

https://laravel.com/api/5.8/Illuminate/Console/Command.html#method_ask

mixed ask(string $question, string|null $default = null)
于 2021-05-08T01:03:44.233 回答