Laravel配置Route调用artisan

    //web调用artisan
    Route::get('/artisan', function(\Illuminate\Http\Request $request) {
        $all = \Illuminate\Support\Facades\Artisan::all();
        $cmd = $request->get('cmd', '');
        if (empty($cmd) or !isset($all[$cmd])) {
            return response()->make('404 Not Found', 404);
        }
        // key => value
        $params = [];
        foreach ($request->query() as $k => $v) {
            if ($k == 'cmd') {
                continue;
            }
            if (is_numeric($k)) {
                $params[] = $k;
                continue;
            }
            $params['--'.$k] = $v;
        }
        \Illuminate\Support\Facades\Artisan::call($cmd, $params);

        return response()->make('done');
    });

比如有artisan console任务

class SystemMail extends Command
{
    protected $signature = 'system_mail_send {--email=}';
}

Web调用方法

http://www.host.com/route/artisan?cmd=system_mail_send&email=abc@qq.com
  • 哈哈,超级入口,只是不支持artisan的argument,只支持option

为安全防范,可以在route加入JWT的token验证

上一篇:Laravel5.5 实现session配置 (转)


下一篇:安全系列 —— 登录认证