Laravel Artisan 命令行:单行输出 3 个改进

问题

我想在命令执行时,向控制台显示输出一些信息,该怎么做?

回答

在命令中,我们可以使用 lineinfocommentquestionerror 方法来向控制台显示输出信息。每个方法使用不同的前景色和背景色组合来实现不同的显示效果。考虑以下例子:

Artisan::command('demo:output', function () {
    $this->line('line <info>info</info> <error>error</error> <comment>comment</comment> <question>question</question>');
    $this->info('This is info');
    $this->error('This is error');
    $this->comment('This is comment');
    $this->question('This is question');
    $this->line("1\n2\n");
})->describe('Demo output method effect');

显示效果:

从上例中也可看到,如果我们想在一行中输出不同效果的文字,可以将方法名转换成类似 html 标签的方式来实现,不过,line 方法没有对应的标签实现。。

参考

本文为 Wiki 文章,邀您参与纠错、纰漏和优化
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!