/** * [api 内部请求] * @author Foreach * @param string $method [请求方式] * @param string $url [地址] * @param array $params [参数] * @param integer $instance * @param integer $original * @return */function api($method = 'POST', $url = '', $params = array(), $instance = 1, $original = 1){ $request = Request::create($url, $method, $params); $request->headers->add(['Accept' => 'application/json', 'Authorization' => 'Bearer '. Session::get('token')]); // 注入JWT请求单例 app()->resolving(\Tymon\JWTAuth\JWT::class, function ($jwt) use ($request) { $jwt->setRequest($request); return $jwt; }); Auth::guard('api')->setRequest($request); // 解决获取认证用户 $request->setUserResolver(function() { return Auth::user('api'); }); // 解决请求传参问题 if ($instance) { // 获取登录用户不需要传参 app()->instance(Request::class, $request); } $response = Route::dispatch($request); return $original ? $response->original : $response;}
调用: