laravel 控制器中的工匠呼叫输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37726558/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:58:00  来源:igfitidea点击:

Artisan Call output in Controller?

phplaravellaravel-5

提问by user1469734

I have a complex Artisan Command that I wanna call in my Controller also. That works. Except that it return an Exitcode instead of output.

我有一个复杂的 Artisan 命令,我也想在我的控制器中调用它。那个有效。除了它返回一个退出代码而不是输出。

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 ""
}

I want the output of the command. How to do that? The last line of my Artisan command has a return on the last line that should be returned.. How?

我想要命令的输出。怎么做?我的 Artisan 命令的最后一行在应该返回的最后一行上有一个返回......如何?

回答by zorx

$command = 'foo:bar';

$params = [
        'datum' => $request->get('date'),
];

Artisan::call($command, $params);
dd(Artisan::output());

回答by Farid shahidi

Some off commands can not run with php artisan in controller you need to run them with shell

某些关闭命令无法在控制器中使用 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>';
    }

This is the list of commands and api:gen and passport install just will run with shell from /bootstrap folder !

这是命令列表,api:gen 和passport install 将使用/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',
            ],
        ];

回答by Software Developer

code to output inspire phrases instead of exit code

输出激励短语而不是退出代码的代码

Route::get('/wisdom', function (Request $request) {
   Artisan::call('inspire');
   return Artisan::output();
});