<?php
namespace App\Console\Commands;
trait PrependsOutput
{
public function line($string)
{
parent::line($this->prepend($string));
}
public function comment($string)
{
parent::comment($this->prepend($string));
}
public function error($string)
{
parent::error($this->prepend($string));
}
public function info($string)
{
parent::info($this->prepend($string));
}
public function warn($string)
{
parent::warn($this->prepend($string));
}
protected function prepend($string)
{
if (method_exists($this, 'getPrependString')) {
return $this->getPrependString($string).$string;
}
return $string;
}
}
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class MyCommand extends Command
{
use PrependsOutput, PrependsTimestamp;
protected $signature = 'mycommand';
protected $description = '';
// you can override the default format
// protected $outputTimestampFormat = '(m/d/Y H:i:s)';
public function handle()
{
$this->comment('comment');
$this->info('info');
$this->warn('warn');
$this->error('error');
$this->line('line');
}
}
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class MyCommand extends Command
{
use PrependsOutput, PrependsTimestamp;
protected $signature = 'mycommand';
protected $description = '';
// you can override the default format
// protected $outputTimestampFormat = '(m/d/Y H:i:s)';
public function handle()
{
$this->comment('comment');
$this->info('info');
$this->warn('warn');
$this->error('error');
$this->line('line');
}
}
3条答案
按热度按时间3xiyfsfu1#
一种方法来做到这一点(假设你在Laravel 5.0+上):
PrependsOutput.php
字符串
PrependsTimestamp.php
型
在你的命令下:
型
结果:
的数据
6ss1mwsb2#
从laravel 5.5开始,你可以添加这个特性。
并在控制台命令中使用它
字符串
相同输出:
的数据
4bbkushb3#
@peterm的答案针对Laravel 5.8(可能更早)进行了调整:
line()
的父方法签名。line()
足以为所有输出类型前置文本PrependsOutput.php
字符串
PrependsTimestamp.php
型
在你的命令下:
型
结果:
x1c 0d1x的数据