{"id":25170,"date":"2024-11-21T08:03:42","date_gmt":"2024-11-21T00:03:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25170\/"},"modified":"2024-11-21T08:03:42","modified_gmt":"2024-11-21T00:03:42","slug":"%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86laravel-api%e6%8a%a5%e9%94%99%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25170\/","title":{"rendered":"\u5982\u4f55\u5904\u7406Laravel API\u62a5\u9519\u95ee\u9898"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/170971668480395.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5904\u7406Laravel API\u62a5\u9519\u95ee\u9898\u63d2\u56fe\" alt=\"\u5982\u4f55\u5904\u7406Laravel API\u62a5\u9519\u95ee\u9898\u63d2\u56fe\" \/><\/p>\n<p>\u6807\u9898\uff1a\u5982\u4f55\u5904\u7406Laravel API\u62a5\u9519\u95ee\u9898\uff0c\u9700\u8981\u5177\u4f53\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>\u5728\u8fdb\u884c Laravel \u5f00\u53d1\u65f6\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230 API \u62a5\u9519\u7684\u60c5\u51b5\u3002\u8fd9\u4e9b\u62a5\u9519\u53ef\u80fd\u6765\u81ea\u4e8e\u7a0b\u5e8f\u4ee3\u7801\u903b\u8f91\u9519\u8bef\u3001\u6570\u636e\u5e93\u67e5\u8be2\u95ee\u9898\u6216\u662f\u5916\u90e8 API \u8bf7\u6c42\u5931\u8d25\u7b49\u591a\u79cd\u539f\u56e0\u3002\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u62a5\u9519\u662f\u4e00\u4e2a\u5173\u952e\u7684\u95ee\u9898\uff0c\u672c\u6587\u5c06\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u6f14\u793a\u5982\u4f55\u6709\u6548\u5904\u7406 Laravel API \u62a5\u9519\u95ee\u9898\u3002<\/p>\n<h3>1. \u9519\u8bef\u5904\u7406<\/h3>\n<p>\u5728 Laravel \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u548c\u9519\u8bef\u5904\u7406\u5668\u6765\u6709\u6548\u5730\u5904\u7406 API \u62a5\u9519\u95ee\u9898\u3002\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u5668\u6765\u6355\u83b7\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5e76\u8fd4\u56de\u5bf9\u5e94\u7684\u9519\u8bef\u4fe1\u606f\u7ed9\u5ba2\u6237\u7aef\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5f02\u5e38\u5904\u7406\u5668\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php namespace AppExceptions;\n\nuse Exception;\nuse IlluminateFoundationExceptionsHandler as ExceptionHandler;\nuse IlluminateHttpJsonResponse;\n\nclass Handler extends ExceptionHandler\n{\n    public function render($request, Exception $exception)\n    {\n        if ($exception instanceof SymfonyComponentHttpKernelExceptionNotFoundHttpException) {\n            return response()-&gt;json(['error' =&amp;gt; 'Not found'], 404);\n        }\n\n        if ($exception instanceof IlluminateDatabaseEloquentModelNotFoundException) {\n            return response()-&amp;gt;json(['error' =&amp;gt; 'Resource not found'], 404);\n        }\n\n        return parent::render($request, $exception);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>2. \u6570\u636e\u9a8c\u8bc1\u9519\u8bef\u5904\u7406<\/h3>\n<p>\u5728\u5904\u7406\u7528\u6237\u8bf7\u6c42\u65f6\uff0c\u6570\u636e\u9a8c\u8bc1\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Laravel \u7684\u9a8c\u8bc1\u5668\u6765\u5904\u7406\u8f93\u5165\u6570\u636e\u7684\u6709\u6548\u6027\u3002\u5982\u679c\u9a8c\u8bc1\u5931\u8d25\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd4\u56de\u5305\u542b\u9519\u8bef\u4fe1\u606f\u7684 JSON \u54cd\u5e94\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6570\u636e\u9a8c\u8bc1\u9519\u8bef\u5904\u7406\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>public function store(Request $request)\n{\n    $validator = Validator::make($request-&amp;gt;all(), [\n        'name' =&amp;gt; 'required',\n        'email' =&amp;gt; 'required|email',\n    ]);\n\n    if ($validator-&amp;gt;fails()) {\n        return response()-&amp;gt;json(['error' =&amp;gt; $validator-&amp;gt;errors()], 422);\n    }\n\n    \/\/ \u5904\u7406\u8bf7\u6c42\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>3. \u6570\u636e\u5e93\u67e5\u8be2\u9519\u8bef\u5904\u7406<\/h3>\n<p>\u5728\u8fdb\u884c\u6570\u636e\u5e93\u67e5\u8be2\u65f6\uff0c\u6709\u53ef\u80fd\u4f1a\u9047\u5230\u67e5\u8be2\u5931\u8d25\u7684\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528 Laravel \u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u83b7\u6570\u636e\u5e93\u67e5\u8be2\u5f02\u5e38\u5e76\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u6570\u636e\u5e93\u67e5\u8be2\u9519\u8bef\u5904\u7406\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>try {\n    $user = User::findOrFail($id);\n} catch (IlluminateDatabaseEloquentModelNotFoundException $e) {\n    return response()-&amp;gt;json(['error' =&amp;gt; 'User not found'], 404);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>4. \u5916\u90e8 API \u8bf7\u6c42\u5931\u8d25\u5904\u7406<\/h3>\n<p>\u5728\u8fdb\u884c\u5916\u90e8 API \u8bf7\u6c42\u65f6\uff0c\u5e38\u5e38\u4f1a\u9047\u5230\u7f51\u7edc\u8fde\u63a5\u95ee\u9898\u6216\u8005\u540e\u7aef\u670d\u52a1\u6545\u969c\u7b49\u60c5\u51b5\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Laravel \u63d0\u4f9b\u7684 Guzzle HTTP \u5ba2\u6237\u7aef\u6765\u8fdb\u884c\u5916\u90e8 API \u8bf7\u6c42\uff0c\u5e76\u901a\u8fc7 try-catch \u5757\u6765\u6355\u83b7\u8bf7\u6c42\u5931\u8d25\u7684\u5f02\u5e38\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5916\u90e8 API \u8bf7\u6c42\u5931\u8d25\u5904\u7406\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>try {\n    $response = Http::get('https:\/\/api.example.com\/data');\n    $data = $response-&amp;gt;json();\n} catch (Exception $e) {\n    return response()-&amp;gt;json(['error' =&amp;gt; 'API request failed'], 500);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>\u7ed3\u8bed<\/h3>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5728 Laravel \u4e2d\u5982\u4f55\u5904\u7406 API \u62a5\u9519\u95ee\u9898\uff0c\u4ece\u5f02\u5e38\u5904\u7406\u3001\u6570\u636e\u9a8c\u8bc1\u9519\u8bef\u3001\u6570\u636e\u5e93\u67e5\u8be2\u9519\u8bef\u5230\u5916\u90e8 API \u8bf7\u6c42\u5931\u8d25\u5904\u7406\uff0c\u90fd\u6709\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\uff0c\u4ee5\u63d0\u9ad8 API \u7684\u7a33\u5b9a\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u5904\u7406 Laravel API \u62a5\u9519\u95ee\u9898\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5904\u7406Laravel API\u62a5\u9519\u95ee\u9898\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6807\u9898\uff1a\u5982\u4f55\u5904\u7406Laravel API\u62a5\u9519\u95ee\u9898\uff0c\u9700\u8981\u5177\u4f53\u4ee3\u7801\u793a\u4f8b \u5728\u8fdb\u884c Laravel \u5f00\u53d1\u65f6\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230 API \u62a5\u9519\u7684\u60c5\u51b5\u3002\u8fd9\u4e9b\u62a5\u9519\u53ef\u80fd\u6765\u81ea\u4e8e\u7a0b\u5e8f\u4ee3\u7801\u903b\u8f91\u9519\u8bef\u3001\u6570\u636e\u5e93\u67e5\u8be2\u95ee\u9898\u6216\u662f\u5916\u90e8 API \u8bf7\u6c42\u5931\u8d25\u7b49\u591a\u79cd\u539f\u56e0\u3002\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u62a5\u9519\u662f\u4e00\u4e2a\u5173\u952e\u7684\u95ee\u9898\uff0c\u672c\u6587\u5c06\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u6f14\u793a\u5982\u4f55\u6709\u6548\u5904\u7406 Laravel API \u62a5\u9519\u95ee\u9898\u3002 1. \u9519\u8bef\u5904\u7406 \u5728 Laravel \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u548c\u9519\u8bef\u5904\u7406\u5668\u6765\u6709\u6548\u5730\u5904\u7406 API \u62a5\u9519\u95ee\u9898\u3002\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u5668\u6765\u6355\u83b7\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5e76\u8fd4\u56de\u5bf9\u5e94\u7684\u9519\u8bef\u4fe1\u606f\u7ed9\u5ba2\u6237\u7aef\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5f02\u5e38\u5904\u7406\u5668\u4ee3\u7801\u793a\u4f8b\uff1a &lt;?php namespace AppExceptions; use Exception; use IlluminateFoundationExceptionsHandler as ExceptionHandler; use IlluminateHttpJsonResponse; class Handler extends ExceptionHandler { public function render($request, Exception $exception) { if ($exception instanceof SymfonyComponentHttpKernelExceptionNotFoundHttpException) { return response()-&gt;json([&#8216;error&#8217; =&amp;gt; &#8216;Not found&#8217;], 404); } if ($exception [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-25170","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=25170"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25170\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}