通过 URL 调用 Artisan 命令

Call Artisan Command via URL

在我的路线中有

Route::get('artisan/{command}/{param}', 'CacheController@show');

在我的控制器中我有

public function show($id, $param)
{
    $artisan = Artisan::call($id,['flag'=>$param]);
    $output = Artisan::output();
    return $output;
}

我希望能够通过访问 domain.com/artisan/cache/clearroute/cache 来调用 route:cachecache:clear 但是当我调用它们时它返回了类似这样的内容

Command "cache" is not defined.

只调用缓存,不调用cache:clear有什么可能出错?

像这样修改你的代码:

    $artisan = \Artisan::call($command.":".$param);
    $output = \Artisan::output();
    return $output;

标志参数用于传递artisan命令的参数。

你在调用 id 时又调用了它 command

因此您的函数需要如下所示

public function show($command, $param) {
    $artisan = Artisan::call($command,['flag'=>$param]);
    $output = Artisan::output();
    return $output;
}

所以你的 URL 看起来像这样 domain.com/artisan/cache/clear 这意味着你正在调用这条路线

Route::get('artisan/{command}/{param}', 'CacheController@show');

所以你需要 $command 而不是 $id