{"id":25268,"date":"2024-11-21T09:21:38","date_gmt":"2024-11-21T01:21:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25268\/"},"modified":"2024-11-21T09:21:38","modified_gmt":"2024-11-21T01:21:38","slug":"laravel%e6%80%8e%e4%b9%88%e5%86%99%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25268\/","title":{"rendered":"laravel\u600e\u4e48\u5199\u63a5\u53e3"},"content":{"rendered":"<blockquote><p>\n  \u5982\u4f55\u4f7f\u7528 laravel \u7f16\u5199 api \u63a5\u53e3\u5728 laravel \u4e2d\u7f16\u5199 api \u63a5\u53e3\u9700\u8981\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u5b8c\u6210 laravel \u7684\u5b89\u88c5\u548c\u914d\u7f6e\u3002\u5728 routes\/api.php \u4e2d\u6ce8\u518c api \u8def\u7531\u3002\u5728 app\/http\/controllers \u4e2d\u521b\u5efa\u63a7\u5236\u5668\u5e76\u5b9a\u4e49\u65b9\u6cd5\u6765\u5904\u7406 api \u8bf7\u6c42\u3002\u4f7f\u7528 response()-&gt;json() \u51fd\u6570\u8fd4\u56de json \u54cd\u5e94\u3002\u8bbe\u7f6e\u9002\u5f53\u7684 http \u72b6\u6001\u7801\u4ee5\u6307\u793a\u54cd\u5e94\u72b6\u6001\u3002\u4f7f\u7528 json_encode() \u51fd\u6570\u8fd4\u56de\u7279\u5b9a\u4fe1\u606f\uff0c\u4f8b\u5982\u54cd\u5e94\u6d88\u606f\u6216\u9519\u8bef\u3002\u4f7f\u7528 postman \u6216 i\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202404\/09\/2024040914572677895.jpg\" class=\"aligncenter\" title=\"laravel\u600e\u4e48\u5199\u63a5\u53e3\u63d2\u56fe\" alt=\"laravel\u600e\u4e48\u5199\u63a5\u53e3\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Laravel \u7f16\u5199 API \u63a5\u53e3<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><br \/>Laravel \u662f\u4e00\u4e2a\u6d41\u884c\u7684 PHP \u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u6784\u5efa RESTful API \u63a5\u53e3\u7684\u5f3a\u5927\u529f\u80fd\u3002\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u5982\u4f55\u4f7f\u7528 Laravel \u7f16\u5199 API \u63a5\u53e3\uff0c\u5305\u62ec\u8bbe\u7f6e\u3001\u8def\u7531\u3001\u63a7\u5236\u5668\u548c\u54cd\u5e94\u3002<\/p>\n<p><strong>\u8bbe\u7f6e<\/strong><\/p>\n<ol>\n<li>\u5b89\u88c5 Laravel\uff1a\u4f7f\u7528 Composer \u5b89\u88c5 Laravel\uff0c\u6216\u4f7f\u7528 Laravel Installer\u3002<\/li>\n<li>\u521b\u5efa\u65b0\u9879\u76ee\uff1a\u521b\u5efa\u65b0 Laravel \u9879\u76ee\u3002<\/li>\n<li>\u542f\u7528\u8def\u7531\uff1a\u5728 app\/Providers\/RouteServiceProvider.php \u4e2d\u53d6\u6d88\u6ce8\u91ca Route::resource(&#8216;api\/*&#8217;, &#8216;ApiController&#8217;);\u3002<\/li>\n<\/ol>\n<p><strong>\u8def\u7531<\/strong><br \/>API \u8def\u7531\u901a\u5e38\u4ee5 \/api \u4e3a\u524d\u7f00\u3002\u5728 routes\/api.php \u4e2d\u6ce8\u518c\u8def\u7531\uff1a<\/p>\n<pre>Route::get('\/users', 'UserController@index');\nRoute::post('\/users', 'UserController@store');\nRoute::put('\/users\/{id}', 'UserController@update');\nRoute::delete('\/users\/{id}', 'UserController@destroy');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u63a7\u5236\u5668<\/strong><br \/>\u63a7\u5236\u5668\u8d1f\u8d23\u5904\u7406 API \u8bf7\u6c42\u3002\u521b\u5efa app\/Http\/Controllers\/ApiController.php \u5e76\u5b9a\u4e49\u65b9\u6cd5\uff1a<\/p>\n<pre>class ApiController extends Controller\n{\n    public function index() { return response()-&gt;json(User::all()); }\n    public function store(Request $request) { return response()-&gt;json(User::create($request-&gt;all())); }\n    public function update(Request $request, $id) { return response()-&gt;json(User::find($id)-&gt;update($request-&gt;all())); }\n    public function destroy($id) { return response()-&gt;json(User::find($id)-&gt;delete()); }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u54cd\u5e94<\/strong><br \/>\u63a7\u5236\u5668\u65b9\u6cd5\u8fd4\u56de JSON \u54cd\u5e94\u3002response()-&gt;json() \u51fd\u6570\u53ef\u4ee5\u5c06\u6570\u636e\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u3002\u8bbe\u7f6e\u9002\u5f53\u7684 HTTP \u72b6\u6001\u7801\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>return response()-&gt;json(User::all(), 200); \/\/ OK\nreturn response()-&gt;json(User::create($request-&gt;all()), 201); \/\/ Created\nreturn response()-&gt;json(User::find($id)-&gt;update($request-&gt;all()), 200); \/\/ OK\nreturn response()-&gt;json(User::find($id)-&gt;delete(), 204); \/\/ No Content<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8fd4\u56de\u7279\u5b9a\u4fe1\u606f<\/strong><br \/>\u5982\u679c\u60a8\u9700\u8981\u8fd4\u56de\u7279\u5b9a\u4fe1\u606f\uff0c\u4f8b\u5982\u54cd\u5e94\u6d88\u606f\u6216\u9519\u8bef\uff0c\u53ef\u4ee5\u4f7f\u7528 json_encode() \u51fd\u6570\uff1a<\/p>\n<pre>return response()-&gt;json(['message' =&gt; 'Success'], 200);\nreturn response()-&gt;json(['error' =&gt; 'User not found'], 404);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6d4b\u8bd5 API<\/strong><br \/>\u4f7f\u7528\u5de5\u5177\u5982 Postman \u6216 Insomnia \u6d4b\u8bd5\u60a8\u7684 API\u3002\u53d1\u9001\u8bf7\u6c42\u5e76\u9a8c\u8bc1\u54cd\u5e94\u662f\u5426\u7b26\u5408\u9884\u671f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u5199\u63a5\u53e3\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>\u5982\u4f55\u4f7f\u7528 laravel \u7f16\u5199 api \u63a5\u53e3\u5728 laravel \u4e2d\u7f16\u5199 api \u63a5\u53e3\u9700\u8981\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u5b8c\u6210 laravel \u7684\u5b89\u88c5\u548c\u914d\u7f6e\u3002\u5728 routes\/api.php \u4e2d\u6ce8\u518c api \u8def\u7531\u3002\u5728 app\/http\/controllers \u4e2d\u521b\u5efa\u63a7\u5236\u5668\u5e76\u5b9a\u4e49\u65b9\u6cd5\u6765\u5904\u7406 api \u8bf7\u6c42\u3002\u4f7f\u7528 response()-&gt;json() \u51fd\u6570\u8fd4\u56de json \u54cd\u5e94\u3002\u8bbe\u7f6e\u9002\u5f53\u7684 http \u72b6\u6001\u7801\u4ee5\u6307\u793a\u54cd\u5e94\u72b6\u6001\u3002\u4f7f\u7528 json_encode() \u51fd\u6570\u8fd4\u56de\u7279\u5b9a\u4fe1\u606f\uff0c\u4f8b\u5982\u54cd\u5e94\u6d88\u606f\u6216\u9519\u8bef\u3002\u4f7f\u7528 postman \u6216 i \u5982\u4f55\u4f7f\u7528 Laravel \u7f16\u5199 API \u63a5\u53e3 \u7b80\u4ecbLaravel \u662f\u4e00\u4e2a\u6d41\u884c\u7684 PHP \u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u6784\u5efa RESTful API \u63a5\u53e3\u7684\u5f3a\u5927\u529f\u80fd\u3002\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u5982\u4f55\u4f7f\u7528 Laravel \u7f16\u5199 API \u63a5\u53e3\uff0c\u5305\u62ec\u8bbe\u7f6e\u3001\u8def\u7531\u3001\u63a7\u5236\u5668\u548c\u54cd\u5e94\u3002 \u8bbe\u7f6e \u5b89\u88c5 Laravel\uff1a\u4f7f\u7528 Composer \u5b89\u88c5 Laravel\uff0c\u6216\u4f7f\u7528 Laravel Installer\u3002 \u521b\u5efa\u65b0\u9879\u76ee\uff1a\u521b\u5efa\u65b0 [&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-25268","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25268","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=25268"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25268\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}