{"id":48456,"date":"2024-12-02T12:09:08","date_gmt":"2024-12-02T04:09:08","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48456\/"},"modified":"2024-12-02T12:09:08","modified_gmt":"2024-12-02T04:09:08","slug":"php%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e6%8c%87%e5%8d%97%ef%bc%9arestful-api%e5%bc%80%e5%8f%91%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48456\/","title":{"rendered":"php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aRESTful API\u5f00\u53d1\u6307\u5357"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aRESTful API\u5f00\u53d1\u6307\u5357<\/h1>\n<p><span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u662f<\/span><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aphp\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aRESTful API\u5f00\u53d1\u6307\u5357\u300b<\/span>\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01<\/span><\/p>\n<p>\u5982\u4f55\u4f7f\u7528 Laravel \u521b\u5efa RESTful API\uff1f\u5b89\u88c5 Laravel\u3002\u914d\u7f6e\u8def\u7531\uff0c\u901a\u8fc7 routes\/api.php \u5b9a\u4e49 CRUD \u64cd\u4f5c\u8def\u7531\u3002\u521b\u5efa\u63a7\u5236\u5668 TodoController\uff0c\u7528\u4e8e\u5904\u7406 CRUD \u64cd\u4f5c\u3002\u6dfb\u52a0\u4ee3\u7801\u6ce8\u91ca\uff0c\u4ee5\u6e05\u695a\u5730\u63cf\u8ff0\u6bcf\u4e2a\u64cd\u4f5c\u7684\u529f\u80fd\u3002\u4f7f\u7528 Postman \u5b89\u88c5\u5e76\u8c03\u7528 API\uff0c\u4ee5\u9a8c\u8bc1\u5176\u529f\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730031923671e31338788b.jpg\" class=\"aligncenter\" title=\"php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aRESTful API\u5f00\u53d1\u6307\u5357\u63d2\u56fe\" alt=\"php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aRESTful API\u5f00\u53d1\u6307\u5357\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aRESTful API \u5f00\u53d1\u6307\u5357<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>REST\uff08Representational State Transfer\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u6784\u5efa\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u7684\u4f53\u7cfb\u7ed3\u6784\u98ce\u683c\u3002\u5b83\u4f7f\u7528 HTTP \u8bf7\u6c42\u548c\u54cd\u5e94\u6765\u5b9e\u73b0\u8d44\u6e90\u7684\u521b\u5efa\u3001\u8bfb\u53d6\u3001\u66f4\u65b0\u548c\u5220\u9664 (CRUD) \u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u521b\u5efa CRUD RESTful API<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 PHP \u6846\u67b6 Laravel \u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684 CRUD RESTful API\uff1a<\/p>\n<p><strong>\u5b89\u88c5 Laravel<\/strong><\/p>\n<pre>composer global require laravel\/installer\nlaravel new my-api<\/pre>\n<p><strong>\u914d\u7f6e\u8def\u7531<\/strong><\/p>\n<p>\u5728 <code>routes\/api.php<\/code> \u4e2d\u5b9a\u4e49\u8def\u7531\uff1a<\/p>\n<pre>Route::resource('todos', 'TodoController');<\/pre>\n<p><strong>\u521b\u5efa\u63a7\u5236\u5668<\/strong><\/p>\n<p>\u5728 <code>app\/Http\/Controllers\/TodoController.php<\/code> \u4e2d\u521b\u5efa\u63a7\u5236\u5668\uff1a<\/p>\n<pre>namespace App\\Http\\Controllers;\n\nuse App\\Todo;\nuse Illuminate\\Http\\Request;\n\nclass TodoController extends Controller\n{\n    public function index()\n    {\n        return Todo::all();\n    }\n\n    public function store(Request $request)\n    {\n        Todo::create($request-&gt;all());\n        return response('', 201);\n    }\n\n    public function show(Todo $todo)\n    {\n        return $todo;\n    }\n\n    public function update(Request $request, Todo $todo)\n    {\n        $todo-&gt;update($request-&gt;all());\n        return $todo;\n    }\n\n    public function destroy(Todo $todo)\n    {\n        $todo-&gt;delete();\n        return response('', 204);\n    }\n}<\/pre>\n<p><strong>\u589e\u52a0\u4ee3\u7801\u6ce8\u91ca<\/strong><\/p>\n<pre>\/**\n * Display a listing of the resource.\n *\n * @return \\Illuminate\\Http\\Response\n *\/\npublic function index()\n{\n    return Todo::all();\n}\n\n\/**\n * Store a newly created resource in storage.\n *\n * @param  \\Illuminate\\Http\\Request  $request\n * @return \\Illuminate\\Http\\Response\n *\/\npublic function store(Request $request)\n{\n    Todo::create($request-&gt;all());\n    return response('', 201);\n}\n\n\/**\n * Display the specified resource.\n *\n * @param  \\App\\Todo  $todo\n * @return \\Illuminate\\Http\\Response\n *\/\npublic function show(Todo $todo)\n{\n    return $todo;\n}\n\n\/**\n * Update the specified resource in storage.\n *\n * @param  \\Illuminate\\Http\\Request  $request\n * @param  \\App\\Todo  $todo\n * @return \\Illuminate\\Http\\Response\n *\/\npublic function update(Request $request, Todo $todo)\n{\n    $todo-&gt;update($request-&gt;all());\n    return $todo;\n}\n\n\/**\n * Remove the specified resource from storage.\n *\n * @param  \\App\\Todo  $todo\n * @return \\Illuminate\\Http\\Response\n *\/\npublic function destroy(Todo $todo)\n{\n    $todo-&gt;delete();\n    return response('', 204);\n}<\/pre>\n<p><strong>\u4f7f\u7528 Postman \u6d4b\u8bd5 API<\/strong><\/p>\n<p>\u5b89\u88c5 Postman \u5e76\u5728\u4ee5\u4e0b URL \u8c03\u7528 API\uff1a<\/p>\n<ul>\n<li><code>GET localhost:8000\/api\/todos<\/code><\/li>\n<li><code>POST localhost:8000\/api\/todos<\/code> (\u53d1\u9001 JSON \u6570\u636e)<\/li>\n<li><code>GET localhost:8000\/api\/todos\/:id<\/code><\/li>\n<li><code>PUT localhost:8000\/api\/todos\/:id<\/code> (\u53d1\u9001 JSON \u6570\u636e)<\/li>\n<li><code>DELETE localhost:8000\/api\/todos\/:id<\/code><\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300aphp\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aRESTful API\u5f00\u53d1\u6307\u5357\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aRESTful API\u5f00\u53d1\u6307\u5357 \u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898\u662f\u300aphp\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aRESTful API\u5f00\u53d1\u6307\u5357\u300b\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e\u6587\u7ae0\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01 \u5982\u4f55\u4f7f\u7528 Laravel \u521b\u5efa RESTful API\uff1f\u5b89\u88c5 Laravel\u3002\u914d\u7f6e\u8def\u7531\uff0c\u901a\u8fc7 routes\/api.php \u5b9a\u4e49 CRUD \u64cd\u4f5c\u8def\u7531\u3002\u521b\u5efa\u63a7\u5236\u5668 TodoController\uff0c\u7528\u4e8e\u5904\u7406 CRUD \u64cd\u4f5c\u3002\u6dfb\u52a0\u4ee3\u7801\u6ce8\u91ca\uff0c\u4ee5\u6e05\u695a\u5730\u63cf\u8ff0\u6bcf\u4e2a\u64cd\u4f5c\u7684\u529f\u80fd\u3002\u4f7f\u7528 Postman \u5b89\u88c5\u5e76\u8c03\u7528 API\uff0c\u4ee5\u9a8c\u8bc1\u5176\u529f\u80fd\u3002 PHP \u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aRESTful API \u5f00\u53d1\u6307\u5357 \u7b80\u4ecb REST\uff08Representational State Transfer\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u6784\u5efa\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u7684\u4f53\u7cfb\u7ed3\u6784\u98ce\u683c\u3002\u5b83\u4f7f\u7528 HTTP \u8bf7\u6c42\u548c\u54cd\u5e94\u6765\u5b9e\u73b0\u8d44\u6e90\u7684\u521b\u5efa\u3001\u8bfb\u53d6\u3001\u66f4\u65b0\u548c\u5220\u9664 (CRUD) \u64cd\u4f5c\u3002 \u5b9e\u6218\u6848\u4f8b\uff1a\u521b\u5efa CRUD RESTful API \u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 PHP \u6846\u67b6 Laravel \u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684 CRUD RESTful API\uff1a \u5b89\u88c5 Laravel composer global require laravel\/installer laravel new my-api \u914d\u7f6e\u8def\u7531 \u5728 [&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-48456","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48456","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=48456"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48456\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}