Laravel Artisan 命令行:单行输出
问题
我想在命令执行时,向控制台显示输出一些信息,该怎么做?
回答
在命令中,我们可以使用 line 、 info 、 comment 、 question 和 error 方法来向控制台显示输出信息。每个方法使用不同的前景色和背景色组合来实现不同的显示效果。考虑以下例子:
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 方法没有对应的标签实现。。
Laravel 社区 Wiki
关于 LearnKu
推荐文章: