laravel 获得 Artisan 电话的回复
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22485513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get response from Artisan call
提问by cenob8
When I run in terminal php artisan migrate
this results in 'Nothing to migrate' when indeed there is nothing to migrate.
当我在终端中运行时,php artisan migrate
这会导致“没有什么可迁移”,而实际上没有什么可迁移的。
When I use Artisan::call('migrate')
in code (use this in a custom Artisan command) this doesn't return any message. It just executes the code without any feedback.
当我Artisan::call('migrate')
在代码中使用(在自定义 Artisan 命令中使用它)时,这不会返回任何消息。它只是执行代码而没有任何反馈。
If I vardump()
the result of the Artisan::call
method it just returns an int(0)
如果我vardump()
是该Artisan::call
方法的结果,它只返回一个int(0)
Is it possible to get the response of the Artisan call method?
是否可以得到Artisan调用方法的响应?
采纳答案by Jeff Lambert
The return result of all commands is defined in the class Symfony\Component\Console\Command\Command
, method run
:
所有命令的返回结果在类Symfony\Component\Console\Command\Command
、方法中定义run
:
return is_numeric($statusCode) ? (int) $statusCode : 0;
The $statusCode
variable is set by calling the command's execute
method, which in artisan's case is defined in the class Illuminate\Console\Command
:
该$statusCode
变量是通过调用命令的execute
方法来设置的,在 artisan 的情况下,它是在类中定义的Illuminate\Console\Command
:
protected function execute(InputInterface $input, OutputInterface $output)
{
return $this->fire();
}
The result of the fire
method is left up to the individual commands, in the case of php artisan migrate
command, nothing is returned from the method so the $statusCode
is null (which is why you get the 0 returned from Symfony\Component\Console\Command\Command::run
method)
该fire
方法的结果由各个命令决定,在命令的情况下,php artisan migrate
该方法不会返回任何内容,因此该方法$statusCode
为空(这就是您从Symfony\Component\Console\Command\Command::run
方法返回 0 的原因)
If you want to get a response back from a custom command, just return an integer back from your fire
method and it will bubble back up into the $statusCode
. You can use that to programmatically switch against different results of your custom command.
如果您想从自定义命令中获得响应,只需从您的fire
方法中返回一个整数,它就会冒泡回$statusCode
. 您可以使用它以编程方式切换自定义命令的不同结果。
If you specifically want to get the result from the artisan:migrate
command, then I don't think there's much you can do to change the return value besides wrapping the command in your own custom command that calls it.
如果您特别想从artisan:migrate
命令中获取结果,那么我认为除了将命令包装在您自己的调用它的自定义命令中之外,您可以做很多事情来更改返回值。
回答by PiTheNumber
For me with Laravel 5.1 all this did not work but you can simply use:
对于使用 Laravel 5.1 的我来说,所有这些都不起作用,但您可以简单地使用:
Artisan::output()
回答by George Huber
I'm able to get the output of Artisan::call() with the via the following:
我可以通过以下方式获得 Artisan::call() 的输出:
use Symfony\Component\Console\Output\StreamOutput;
$stream = fopen("php://output", "w");
Artisan::call("migrate", array(), new StreamOutput($stream));
var_dump($stream);
回答by bishop
Yes, it's possible. To get the output of a built-in artisan command from inside a custom command, pass the OutputStream
from your command into the Artisan::call
. Example:
是的,这是可能的。要从自定义命令中获取内置 artisan 命令的输出,OutputStream
请将命令中的传递到Artisan::call
. 例子:
class MyCommand extends \Illuminate\Console\Command
{
public function fire()
{
\Artisan::call('optimize', [], $this->getOutput());
}
}
回答by mfink
When the Artisan command output you want is issuing an echo
.
当您想要的 Artisan 命令输出发出echo
.
You can access this type of output with ob_startand ob_get_clean.
您可以使用ob_start和ob_get_clean访问这种类型的输出。
For example, if your command echos JSON.
例如,如果您的命令回显 JSON。
Artisan::command('myecho:command', function () {
echo json_encode(config('myconfig'), true);
})->describe('outputs json');
Then you can access the JSON output by wrapping the command call in a buffer:
然后,您可以通过将命令调用包装在缓冲区中来访问 JSON 输出:
\ob_start();
\Artisan::call('myecho:command');
$output = \ob_get_clean();
var_dump($output);
回答by aimme
Late but might be of use to someone searching for the use case.
迟到但可能对搜索用例的人有用。
Let me add how i did it in my tests to print the results to console. my problem was printing output while tests are running migrations. i was using modules and wanted to see the results of the migration process.
让我补充一下我在测试中是如何将结果打印到控制台的。我的问题是在测试运行迁移时打印输出。我正在使用模块并想查看迁移过程的结果。
$this->artisan('module:migrate');
//same as running php artisan module:migrate or
// $this->app['Illuminate\Contracts\Console\Kernel']->call('module:migrate');
echo $this->app['Illuminate\Contracts\Console\Kernel']->output();