我有一个复杂的工匠司令部,我也想在我的控制器中打电话。这样可行。除了它返回Exitcode而不是输出。
use Symfony\Component\Console\Output\BufferedOutput; # on top
public function foobar(Request $request)
{
$this->validate($request, [
'date' => 'required|date_format:Y-m-d',
]);
$output = new BufferedOutput;
$exitCode = Artisan::call('foo:bar', [
'datum' => $request->get('date'),
], $output);
return $exitCode; # returns 0;
return dd($output->fetch()); # returns ""
}
我想要命令的输出。怎么做?我的Artisan命令的最后一行返回了应返回的最后一行..怎么样?
答案 0 :(得分:15)
$command = 'foo:bar';
$params = [
'datum' => $request->get('date'),
];
Artisan::call($command, $params);
dd(Artisan::output());
答案 1 :(得分:0)
一些关闭的命令无法在控制器中与php artisan一起运行,您需要使用shell运行它们
public function getCommand($command)
{
echo '<br> php artisan ' . $command . ' is running...';
$output = new BufferedOutput;
if(strpos($command, 'api') === false && strpos($command, 'passport') === false){
Artisan::call($command, [], $output);
}else{
shell_exec('php ../artisan ' . $command);
dump('php ../artisan ' . $command);
}
dump($output->fetch());
echo 'php artisan ' . $command . ' completed.';
echo '<br><br><a href="/admin/setting/advance">Go back</a>';
}
这是命令列表,并且api:gen和passport安装仅将与/ bootstrap文件夹中的shell一起运行!
$commands = [
[
'id' => 1,
'description' => 'recompile classes',
'command' => 'clear-compiled',
],
[
'id' => 2,
'description' => 'recompile packages',
'command' => 'package:discover',
],
[
'id' => 3,
'description' => 'run backup',
'command' => 'backup:run',
],
[
'id' => 4,
'description' => 'create password for passport',
'command' => 'passport:client --password',
],
[
'id' => 5,
'description' => 'install passport',
'command' => 'passport:install',
],
[
'id' => 6,
'description' => 'create a document for api',
'command' => 'apidoc:generate',
],
[
'id' => 7,
'description' => 'show list of routes',
'command' => 'route:list',
],
[
'id' => 8,
'description' => 'recompile config cache',
'command' => 'config:cache',
],
[
'id' => 9,
'description' => 'clear config cache',
'command' => 'config:clear',
],
[
'id' => 10,
'description' => 'run lastest migrations',
'command' => 'migrate',
],
[
'id' => 11,
'description' => 'run seeders',
'command' => 'db:seed',
],
[
'id' => 12,
'description' => 'recompile route cache',
'command' => 'route:cache',
],
[
'id' => 13,
'description' => 'clear route cache',
'command' => 'route:clear',
],
[
'id' => 14,
'description' => 'recompile view cache',
'command' => 'view:cache',
],
[
'id' => 15,
'description' => 'clear view cache',
'command' => 'view:clear',
],
[
'id' => 16,
'description' => 'optimize all configurations',
'command' => 'optimize',
],
];
答案 2 :(得分:-1)
代码以输出激励短语而不是退出代码
Route::get('/wisdom', function (Request $request) {
Artisan::call('inspire');
return Artisan::output();
});