【问题标题】:Artisan Call output in Controller?控制器中的工匠调用输出?
【发布时间】:2019-12-17 13:21:17
【问题描述】:

我有一个复杂的 Artisan Command,我也想在我的控制器中调用它。这样可行。除了它返回一个退出代码而不是输出。

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 命令的最后一行在应该返回的最后一行有一个返回。如何?

【问题讨论】:

  • Artisan:output 可以帮到你。
  • 为什么该函数包含两个返回语句?为什么不将通过命令运行的逻辑分离到一个公共服务中呢?

标签: php laravel laravel-5


【解决方案1】:
$command = 'foo:bar';

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

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

【讨论】:

  • 为什么只使用 dd()?然后它返回所有$this->info('foobar') 的东西。但我最后会返回一个我想重复使用的数组。
  • dd() 只是为了向您显示响应,如果您想在某处使用结果,那么您所要做的就是return Artisan::output(); 而不是dd(..)
  • 转储(trim(Artisan::output()));如果您不想在那里终止请求,可以使用。
  • 别忘了你可能需要使用use Illuminate\Support\Facades\Artisan; 导入外观才能引用它。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    某些关闭命令不能在控制器中使用 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 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',
                ],
            ];
    

    【讨论】:

    • 谢谢法里德。
    猜你喜欢
    • 2020-12-26
    • 2017-12-26
    • 2017-12-24
    • 2018-04-10
    • 2018-09-08
    • 1970-01-01
    • 2021-05-15
    • 2018-06-24
    • 2016-03-15
    相关资源
    最近更新 更多