{"id":25192,"date":"2024-11-21T09:40:19","date_gmt":"2024-11-21T01:40:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25192\/"},"modified":"2024-11-21T09:40:19","modified_gmt":"2024-11-21T01:40:19","slug":"laravel-api%e6%8a%a5%e9%94%99%e5%b8%b8%e8%a7%81%e5%8e%9f%e5%9b%a0%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25192\/","title":{"rendered":"Laravel API\u62a5\u9519\u5e38\u89c1\u539f\u56e0\u5206\u6790"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/170977626420272.jpg\" class=\"aligncenter\" title=\"Laravel API\u62a5\u9519\u5e38\u89c1\u539f\u56e0\u5206\u6790\u63d2\u56fe\" alt=\"Laravel API\u62a5\u9519\u5e38\u89c1\u539f\u56e0\u5206\u6790\u63d2\u56fe\" \/><\/p>\n<p>Laravel\u662f\u4e00\u4e2a\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u7528\u4e8e\u5feb\u901f\u5f00\u53d1Web\u5e94\u7528\u7a0b\u5e8f\u548cAPI\u3002\u5728\u4f7f\u7528Laravel\u6846\u67b6\u8fdb\u884cAPI\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u5404\u79cd\u62a5\u9519\u548c\u5f02\u5e38\u3002\u8fd9\u4e9b\u62a5\u9519\u53ef\u80fd\u662f\u7531\u4e8e\u4ee3\u7801\u903b\u8f91\u95ee\u9898\u3001\u914d\u7f6e\u9519\u8bef\u6216\u8005\u73af\u5883\u914d\u7f6e\u4e0d\u5f53\u7b49\u539f\u56e0\u5f15\u8d77\u7684\u3002\u4e0b\u9762\u5c06\u9488\u5bf9\u51e0\u79cd\u5e38\u89c1\u7684Laravel API\u62a5\u9519\u8fdb\u884c\u5206\u6790\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<h3>1. 401 Unauthorized<\/h3>\n<p><strong>\u62a5\u9519\u539f\u56e0\uff1a<\/strong> <br \/>401 Unauthorized\u662f\u6307\u5ba2\u6237\u7aef\u8bf7\u6c42\u6ca1\u6709\u63d0\u4f9b\u8eab\u4efd\u9a8c\u8bc1\u4fe1\u606f\u6216\u8005\u63d0\u4f9b\u7684\u8eab\u4efd\u9a8c\u8bc1\u4fe1\u606f\u4e0d\u6b63\u786e\u3002\u8fd9\u4e2a\u9519\u8bef\u901a\u5e38\u53d1\u751f\u5728\u9700\u8981\u7528\u6237\u8ba4\u8bc1\u7684\u63a5\u53e3\u88ab\u672a\u7ecf\u6388\u6743\u7684\u7528\u6237\u8bf7\u6c42\u65f6\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public function getProduct(Request $request, $id)\n{\n    $product = Product::find($id);\n\n    if (!$product) {\n        return response()-&amp;gt;json(['error' =&amp;gt; 'Product not found'], 404);\n    }\n\n    \/\/ \u68c0\u67e5\u7528\u6237\u662f\u5426\u6709\u6743\u9650\u8bbf\u95ee\u8be5\u4ea7\u54c1\n    if (!$request-&amp;gt;user()-&amp;gt;can('view', $product)) {\n        return response()-&amp;gt;json(['error' =&amp;gt; 'Unauthorized'], 401);\n    }\n\n    return response()-&amp;gt;json($product, 200);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>2. 404 Not Found<\/h3>\n<p><strong>\u62a5\u9519\u539f\u56e0\uff1a<\/strong><br \/>404 Not Found\u8868\u793a\u8bf7\u6c42\u7684\u8d44\u6e90\u4e0d\u5b58\u5728\u3002\u5728API\u5f00\u53d1\u4e2d\uff0c\u8fd9\u79cd\u9519\u8bef\u901a\u5e38\u53d1\u751f\u5728\u8bf7\u6c42\u7684\u8def\u7531\u6216\u8005\u8d44\u6e90\u5728\u670d\u52a1\u5668\u7aef\u627e\u4e0d\u5230\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public function getProduct(Request $request, $id)\n{\n    $product = Product::find($id);\n\n    if (!$product) {\n        return response()-&amp;gt;json(['error' =&amp;gt; 'Product not found'], 404);\n    }\n\n    return response()-&amp;gt;json($product, 200);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>3. 500 Internal Server Error<\/h3>\n<p><strong>\u62a5\u9519\u539f\u56e0\uff1a<\/strong><br \/>500 Internal Server Error\u8868\u793a\u670d\u52a1\u5668\u9047\u5230\u65e0\u6cd5\u5904\u7406\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u8fd9\u79cd\u9519\u8bef\u53ef\u80fd\u662f\u7531\u4e8e\u4ee3\u7801\u903b\u8f91\u95ee\u9898\u3001\u6570\u636e\u5e93\u8fde\u63a5\u9519\u8bef\u3001\u670d\u52a1\u5668\u914d\u7f6e\u9519\u8bef\u7b49\u5bfc\u81f4\u7684\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public function createProduct(Request $request)\n{\n    $validatedData = $request-&amp;gt;validate([\n        'name' =&amp;gt; 'required',\n        'price' =&amp;gt; 'required|numeric',\n    ]);\n\n    $product = new Product;\n    $product-&amp;gt;name = $validatedData['name'];\n    $product-&amp;gt;price = $validatedData['price'];\n\n    if ($product-&amp;gt;save()) {\n        return response()-&amp;gt;json($product, 201);\n    } else {\n        return response()-&amp;gt;json(['error' =&amp;gt; 'Failed to create product'], 500);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u4ee3\u7801\u793a\u4f8b\u548c\u5206\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3Laravel API\u62a5\u9519\u5e38\u89c1\u539f\u56e0\u53ca\u89e3\u51b3\u65b9\u6cd5\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u5e94\u8be5\u4ed4\u7ec6\u6392\u67e5\u53ef\u80fd\u5f15\u8d77\u62a5\u9519\u7684\u5730\u65b9\uff0c\u5e76\u53ca\u65f6\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\u6765\u4fee\u590d\u95ee\u9898\uff0c\u4fdd\u8bc1API\u63a5\u53e3\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLaravel API\u62a5\u9519\u5e38\u89c1\u539f\u56e0\u5206\u6790\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>Laravel\u662f\u4e00\u4e2a\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u7528\u4e8e\u5feb\u901f\u5f00\u53d1Web\u5e94\u7528\u7a0b\u5e8f\u548cAPI\u3002\u5728\u4f7f\u7528Laravel\u6846\u67b6\u8fdb\u884cAPI\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u5404\u79cd\u62a5\u9519\u548c\u5f02\u5e38\u3002\u8fd9\u4e9b\u62a5\u9519\u53ef\u80fd\u662f\u7531\u4e8e\u4ee3\u7801\u903b\u8f91\u95ee\u9898\u3001\u914d\u7f6e\u9519\u8bef\u6216\u8005\u73af\u5883\u914d\u7f6e\u4e0d\u5f53\u7b49\u539f\u56e0\u5f15\u8d77\u7684\u3002\u4e0b\u9762\u5c06\u9488\u5bf9\u51e0\u79cd\u5e38\u89c1\u7684Laravel API\u62a5\u9519\u8fdb\u884c\u5206\u6790\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 1. 401 Unauthorized \u62a5\u9519\u539f\u56e0\uff1a 401 Unauthorized\u662f\u6307\u5ba2\u6237\u7aef\u8bf7\u6c42\u6ca1\u6709\u63d0\u4f9b\u8eab\u4efd\u9a8c\u8bc1\u4fe1\u606f\u6216\u8005\u63d0\u4f9b\u7684\u8eab\u4efd\u9a8c\u8bc1\u4fe1\u606f\u4e0d\u6b63\u786e\u3002\u8fd9\u4e2a\u9519\u8bef\u901a\u5e38\u53d1\u751f\u5728\u9700\u8981\u7528\u6237\u8ba4\u8bc1\u7684\u63a5\u53e3\u88ab\u672a\u7ecf\u6388\u6743\u7684\u7528\u6237\u8bf7\u6c42\u65f6\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a public function getProduct(Request $request, $id) { $product = Product::find($id); if (!$product) { return response()-&amp;gt;json([&#8216;error&#8217; =&amp;gt; &#8216;Product not found&#8217;], 404); } \/\/ \u68c0\u67e5\u7528\u6237\u662f\u5426\u6709\u6743\u9650\u8bbf\u95ee\u8be5\u4ea7\u54c1 if (!$request-&amp;gt;user()-&amp;gt;can(&#8216;view&#8217;, $product)) { return response()-&amp;gt;json([&#8216;error&#8217; =&amp;gt; &#8216;Unauthorized&#8217;], 401); } return response()-&amp;gt;json($product, 200); } \u767b\u5f55\u540e\u590d\u5236 2. 404 Not Found \u62a5\u9519\u539f\u56e0\uff1a404 Not Found\u8868\u793a\u8bf7\u6c42\u7684\u8d44\u6e90\u4e0d\u5b58\u5728\u3002\u5728API\u5f00\u53d1\u4e2d\uff0c\u8fd9\u79cd\u9519\u8bef\u901a\u5e38\u53d1\u751f\u5728\u8bf7\u6c42\u7684\u8def\u7531\u6216\u8005\u8d44\u6e90\u5728\u670d\u52a1\u5668\u7aef\u627e\u4e0d\u5230\u7684\u60c5\u51b5\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a public [&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-25192","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25192","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=25192"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25192\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}