取得Artisan电话的回应 [英] Get response from Artisan call
问题描述
当我在终端 php artisan migration
中运行时,当确实没有任何要迁移的内容时,这将导致没有要迁移的内容".
When I run in terminal php artisan migrate
this results in 'Nothing to migrate' when indeed there is nothing to migrate.
当我在代码中使用 Artisan :: call('migrate')
(在自定义Artisan命令中使用此代码)时,此操作不会返回任何消息.它只是执行代码而没有任何反馈.
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
方法的结果进行 vardump()
,则它只会返回 int(0)
If I vardump()
the result of the Artisan::call
method it just returns an int(0)
能否获得Artisan调用方法的响应?
Is it possible to get the response of the Artisan call method?
推荐答案
所有命令的返回结果在类 Symfony \ Component \ Console \ Command \ Command
中定义,方法 run
:
The return result of all commands is defined in the class Symfony\Component\Console\Command\Command
, method run
:
return is_numeric($statusCode) ? (int) $statusCode : 0;
通过调用命令的 execute
方法来设置 $ statusCode
变量,在工匠的情况下,该方法在类 Illuminate \ Console \ Command
:
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
:
protected function execute(InputInterface $input, OutputInterface $output)
{
return $this->fire();
}
fire 方法的结果留给各个命令使用,对于 php artisan migrate
命令而言,该方法不返回任何内容,因此> $ statusCode
为空(这就是为什么您从 Symfony \ Component \ Console \ Command \ Command \ Command :: run
方法返回0的原因)
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
方法返回一个整数,它就会冒泡返回到 $ statusCode
中.您可以使用它来以编程方式切换自定义命令的不同结果.
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.
如果您特别想从 artisan:migrate
命令中获取结果,那么除了将命令包装在自己的自定义命令中之外,我认为您没有太多可以改变返回值的操作会这样.
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电话的回应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!