我有一个运行帮助程序类的控制台命令,我想用以下命令编写输出$this->info()
从助手类到控制台。
我的代码如下所示:
App/Http/Console/Commands/SomeCommand.php
function handle(Helper $helper)
{
return $helper->somefunction();
}
App/Http/SomeHelper.php
function somefunction()
{
//some code
$this->info('send to console');
}
有什么方法可以将帮助程序的输出写入控制台吗?
我终于弄清楚了(适用于 Laravel 5.6)
In the handle()
你的功能一些命令类,添加$this->myHelper->setConsoleOutput($this->getOutput());
.
在您的帮助程序类中,添加:
/**
*
* @var \Symfony\Component\Console\Style\OutputStyle
*/
protected $consoleOutput;
/**
*
* @param \Symfony\Component\Console\Style\OutputStyle $consoleOutput
* @return $this
*/
public function setConsoleOutput($consoleOutput) {
$this->consoleOutput = $consoleOutput;
return $this;
}
/**
*
* @param string $text
* @return $this
*/
public function writeToOuput($text) {
if ($this->consoleOutput) {
$this->consoleOutput->writeln($text);
}
return $this;
}
/**
*
* @param string $text
* @return $this
*/
public function writeErrorToOuput($text) {
if ($this->consoleOutput) {
$style = new \Symfony\Component\Console\Formatter\OutputFormatterStyle('white', 'red', ['bold']); //white text on red background
$this->consoleOutput->getFormatter()->setStyle('error', $style);
$this->consoleOutput->writeln('<error>' . $text . '</error>');
}
return $this;
}
然后,在助手类的其他任何地方,您可以使用:
$this->writeToOuput('Here is a string that I want to show in the console.');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)