得到Artisan电话的回复

时间:2014-03-18 16:35:04

标签: php laravel laravel-4

当我在终端php artisan migrate中投放时,会导致无法迁移'确实没有什么可以迁移的。

当我在代码中使用Artisan::call('migrate')时(在自定义Artisan命令中使用此功能),这并不会返回任何消息。它只是执行代码而没有任何反馈。

如果我vardump() Artisan::call方法的结果,它只会返回int(0)

是否可以获得Artisan调用方法的响应?

5 个答案:

答案 0 :(得分:19)

对于使用Laravel 5.1的所有这些都没有用,但你可以简单地使用:

<Car>
    <Model>Porsche</Model>
</Car>

答案 1 :(得分:8)

所有命令的返回结果在类Symfony\Component\Console\Command\Command中定义,方法run

return is_numeric($statusCode) ? (int) $statusCode : 0;

通过调用命令的$statusCode方法来设置execute变量,该方法在工匠的情况下在类Illuminate\Console\Command中定义:

protected function execute(InputInterface $input, OutputInterface $output)
{
    return $this->fire();
}

fire方法的结果取决于各个命令,在php artisan migrate命令的情况下,方法不返回任何内容,因此$statusCode为空(这是为什么从Symfony\Component\Console\Command\Command::run方法返回0

如果您想从自定义命令获取回复,只需从fire方法返回一个整数,它就会重新回到$statusCode。您可以使用它以编程方式切换自定义命令的不同结果。

如果您特别希望从artisan:migrate命令获得结果,那么除了在您自己调用它的自定义命令中包装命令之外,我认为您无法更改返回值。< / p>

答案 2 :(得分:6)

我能够通过以下内容获得Artisan :: call()的输出:

use Symfony\Component\Console\Output\StreamOutput;

$stream = fopen("php://output", "w");
Artisan::call("migrate", array(), new StreamOutput($stream));

var_dump($stream);

答案 3 :(得分:4)

是的,这是可能的。要从自定义命令中获取内置artisan命令的输出,请将OutputStream从命令传递到Artisan::call。例如:

class MyCommand extends \Illuminate\Console\Command
{
    public function fire()
    {
        \Artisan::call('optimize', [], $this->getOutput());
    }
}

答案 4 :(得分:0)

迟到但可能对搜索用例的人有用。

让我添加我在测试中如何将结果打印到控制台。我的问题是在测试运行迁移时打印输出。我正在使用模块,并希望看到迁移过程的结果。

$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();
相关问题