{"id":22507,"date":"2024-11-21T15:14:25","date_gmt":"2024-11-21T07:14:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22507\/"},"modified":"2024-11-21T15:14:25","modified_gmt":"2024-11-21T07:14:25","slug":"laravel%e5%a6%82%e4%bd%95%e5%86%99api","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22507\/","title":{"rendered":"laravel\u5982\u4f55\u5199api"},"content":{"rendered":"<p>laravel\u4f5c\u4e3a\u4e00\u95e8\u6d41\u884c\u7684php\u6846\u67b6\uff0c\u5df2\u7ecf\u6210\u4e3a\u4e86\u5f00\u53d1\u4eba\u5458\u5199api\u7684\u9996\u9009\u3002\u901a\u8fc7laravel\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u7f16\u5199\u9ad8\u8d28\u91cf\u7684api\uff0c\u5176\u8d8a\u6765\u8d8a\u53d7\u5230\u4f01\u4e1a\u7ea7\u548c\u4e2a\u4eba\u5f00\u53d1\u8005\u7684\u6b22\u8fce\u3002<\/p>\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528Laravel\u7f16\u5199API\u3002\u6211\u4eec\u5c06\u4ece\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u5165\u624b\uff1a\u6784\u5efaAPI\u3001API\u8def\u7531\u3001\u63a7\u5236\u5668\u548cAPI\u6d4b\u8bd5\u3002<\/p>\n<p>\u6784\u5efaAPI<\/p>\n<p>\u5728\u6784\u5efaAPI\u65f6\uff0c\u9700\u8981\u4f7f\u7528Laravel\u63d0\u4f9b\u7684\u5185\u7f6e\u4e2d\u95f4\u4ef6\u548c\u8def\u7531\u5668\u3002\u4eceLaravel 5.5\u5f00\u59cb\uff0c\u6846\u67b6\u5185\u7f6e\u4e86API\u8d44\u6e90\u63a7\u5236\u5668\uff08API Resource Controller\uff09\u529f\u80fd\u3002\u8fd9\u4e2a\u7279\u6027\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u5feb\u7684\u521b\u5efa\u6807\u51c6\u7684REST API\uff0c\u5e76\u4e14\u5e2e\u52a9\u5f00\u53d1\u8005\u7ef4\u62a4\u4ee3\u7801\u4e00\u81f4\u6027\u3002<\/p>\n<p>\u5982\u4f55\u4f7f\u7528\u8d44\u6e90\u63a7\u5236\u5668\uff1f\u9996\u5148\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u65b0\u5efa\u4e00\u4e2a\u63a7\u5236\u5668\uff1a<\/p>\n<pre>php artisan make:controller Api\/PostController --api<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4f1a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aPostController\u7684API\u63a7\u5236\u5668\uff0c\u5b83\u5c06\u81ea\u52a8\u7ee7\u627fLaravel\u7684\u5185\u7f6e\u7684ResourceController\uff0c\u5e76\u4e14\u5305\u542b\u6240\u6709\u7684\u9884\u5b9a\u4e49\u7684\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec\uff1aindex\u3001show\u3001store\u3001update\u548cdestroy\u3002<\/p>\n<p>API\u8def\u7531<\/p>\n<p>\u5728Laravel\u4e2d\uff0c\u8def\u7531\u4e0e\u63a7\u5236\u5668\u7d27\u5bc6\u8026\u5408\uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u5728\u8def\u7531\u4e2d\u5b9a\u4e49\u5904\u7406API\u8bf7\u6c42\u7684\u9876\u7ea7\u63a7\u5236\u5668\u3002<\/p>\n<pre>Route::prefix('api')-&gt;group(function () {\n    Route::resource('posts', 'ApiPostController');\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4e2a\u8def\u7531\u5b9a\u4e49\u4e86\u4e00\u4e2a&#8217;\/api\/posts&#8217;\u7684URL\uff0c\u7528\u6765\u64cd\u4f5cPost\u6a21\u578b\u7684 CRUD \u529f\u80fd\u3002<\/p>\n<p>\u63a7\u5236\u5668<\/p>\n<p>\u5728\u8d44\u6e90\u63a7\u5236\u5668\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9884\u5b9a\u4e49\u7684\u51fd\u6570\u6765\u5904\u7406\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5f00\u59cb\u521b\u5efa\u5e76\u9644\u52a0post\u6587\u7ae0\u7684\u6587\u4ef6\u4e0a\u4f20\uff0c\u5c06\u6587\u4ef6\u4ece\u5b58\u50a8\u5e93\u4e2d\u79fb\u52a8\u5230\u516c\u5171\u76ee\u5f55\u4e2d\uff1a<\/p>\n<pre>public function store(Request $request)\n{\n    $path = $request-&gt;file('photo')-&gt;store('public\/photos');\n    \n    $post = new Post;\n    \n    $post-&gt;title = $request-&gt;input('title');\n    $post-&gt;description = $request-&gt;input('description');\n    $post-&gt;slug = str_slug($request-&gt;input('title'));\n    $post-&gt;photo = $path;\n    \n    $post-&gt;save();\n    \n    return new Resource($post);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>API\u6d4b\u8bd5<\/p>\n<p>\u6d4b\u8bd5API\u662f\u786e\u4fdd\u5176\u529f\u80fd\u6b63\u5e38\u5de5\u4f5c\u7684\u5173\u952e\uff0c\u8bf7\u786e\u4fdd\u5728\u7f16\u5199API\u65f6\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u3002Laravel\u4e5f\u5185\u7f6e\u4e86\u6846\u67b6\u6d4b\u8bd5\u5de5\u5177\u6765\u5e2e\u52a9\u60a8\u8f7b\u677e\u5730\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u3002<\/p>\n<p>\u5728\u4e3aAPI\u7f16\u5199\u6d4b\u8bd5\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u68c0\u67e5\u54cd\u5e94\u7684\u65b9\u6cd5\u6765\u6d4b\u8bd5\u63a7\u5236\u5668\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u6d4b\u8bd5\u63a7\u5236\u5668\u662f\u5426\u53ef\u4ee5\u6b63\u786e\u5730\u83b7\u53d6\u5e16\u5b50\u7684\u5217\u8868\uff1a<\/p>\n<pre>public function testPostIndex()\n{\n    $response = $this-&gt;json('GET', '\/api\/posts');\n    \n    $response-&gt;assertStatus(200);\n    $response-&gt;assertJsonStructure([\n        'data' =&gt; [\n            '*' =&gt; [\n                'id',\n                'title',\n                'description',\n                'slug',\n                'photo',\n                'created_at',\n                'updated_at'\n            ]\n        ]\n    ]);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7ed3\u8bba<\/p>\n<p>\u5728Laravel\u4e2d\uff0c\u7f16\u5199API\u975e\u5e38\u5bb9\u6613\u3002\u4f7f\u7528\u9884\u5b9a\u4e49\u7684API\u63a7\u5236\u5668\u548c\u5185\u7f6e\u7684\u8def\u7531\u5668\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u521b\u5efa\u6807\u51c6\u7684REST API\u3002\u4f7f\u7528Laravel\u7684\u6d4b\u8bd5\u5de5\u5177\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\uff0c\u68c0\u67e5API\u529f\u80fd\u662f\u5426\u6b63\u5e38\u5de5\u4f5c\u3002\u4f18\u79c0\u7684\u6587\u6863\u548c\u793e\u533a\u652f\u6301\uff0c\u662f\u8ba9Laravel\u6210\u4e3a\u5f00\u53d1\u4eba\u5458\u6700\u53d7\u6b22\u8fce\u7684\u6846\u67b6\u4e4b\u4e00\u7684\u5173\u952e\u56e0\u7d20\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel\u5982\u4f55\u5199api\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\u4f5c\u4e3a\u4e00\u95e8\u6d41\u884c\u7684php\u6846\u67b6\uff0c\u5df2\u7ecf\u6210\u4e3a\u4e86\u5f00\u53d1\u4eba\u5458\u5199api\u7684\u9996\u9009\u3002\u901a\u8fc7laravel\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u7f16\u5199\u9ad8\u8d28\u91cf\u7684api\uff0c\u5176\u8d8a\u6765\u8d8a\u53d7\u5230\u4f01\u4e1a\u7ea7\u548c\u4e2a\u4eba\u5f00\u53d1\u8005\u7684\u6b22\u8fce\u3002 \u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528Laravel\u7f16\u5199API\u3002\u6211\u4eec\u5c06\u4ece\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u5165\u624b\uff1a\u6784\u5efaAPI\u3001API\u8def\u7531\u3001\u63a7\u5236\u5668\u548cAPI\u6d4b\u8bd5\u3002 \u6784\u5efaAPI \u5728\u6784\u5efaAPI\u65f6\uff0c\u9700\u8981\u4f7f\u7528Laravel\u63d0\u4f9b\u7684\u5185\u7f6e\u4e2d\u95f4\u4ef6\u548c\u8def\u7531\u5668\u3002\u4eceLaravel 5.5\u5f00\u59cb\uff0c\u6846\u67b6\u5185\u7f6e\u4e86API\u8d44\u6e90\u63a7\u5236\u5668\uff08API Resource Controller\uff09\u529f\u80fd\u3002\u8fd9\u4e2a\u7279\u6027\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u5feb\u7684\u521b\u5efa\u6807\u51c6\u7684REST API\uff0c\u5e76\u4e14\u5e2e\u52a9\u5f00\u53d1\u8005\u7ef4\u62a4\u4ee3\u7801\u4e00\u81f4\u6027\u3002 \u5982\u4f55\u4f7f\u7528\u8d44\u6e90\u63a7\u5236\u5668\uff1f\u9996\u5148\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u65b0\u5efa\u4e00\u4e2a\u63a7\u5236\u5668\uff1a php artisan make:controller Api\/PostController &#8211;api \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4f1a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aPostController\u7684API\u63a7\u5236\u5668\uff0c\u5b83\u5c06\u81ea\u52a8\u7ee7\u627fLaravel\u7684\u5185\u7f6e\u7684ResourceController\uff0c\u5e76\u4e14\u5305\u542b\u6240\u6709\u7684\u9884\u5b9a\u4e49\u7684\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec\uff1aindex\u3001show\u3001store\u3001update\u548cdestroy\u3002 API\u8def\u7531 \u5728Laravel\u4e2d\uff0c\u8def\u7531\u4e0e\u63a7\u5236\u5668\u7d27\u5bc6\u8026\u5408\uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u5728\u8def\u7531\u4e2d\u5b9a\u4e49\u5904\u7406API\u8bf7\u6c42\u7684\u9876\u7ea7\u63a7\u5236\u5668\u3002 Route::prefix(&#8216;api&#8217;)-&gt;group(function () { Route::resource(&#8216;posts&#8217;, &#8216;ApiPostController&#8217;); }); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4e2a\u8def\u7531\u5b9a\u4e49\u4e86\u4e00\u4e2a&#8217;\/api\/posts&#8217;\u7684URL\uff0c\u7528\u6765\u64cd\u4f5cPost\u6a21\u578b\u7684 CRUD \u529f\u80fd\u3002 \u63a7\u5236\u5668 \u5728\u8d44\u6e90\u63a7\u5236\u5668\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9884\u5b9a\u4e49\u7684\u51fd\u6570\u6765\u5904\u7406\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5f00\u59cb\u521b\u5efa\u5e76\u9644\u52a0post\u6587\u7ae0\u7684\u6587\u4ef6\u4e0a\u4f20\uff0c\u5c06\u6587\u4ef6\u4ece\u5b58\u50a8\u5e93\u4e2d\u79fb\u52a8\u5230\u516c\u5171\u76ee\u5f55\u4e2d\uff1a public function store(Request $request) { $path = $request-&gt;file(&#8216;photo&#8217;)-&gt;store(&#8216;public\/photos&#8217;); $post = new Post; $post-&gt;title = $request-&gt;input(&#8216;title&#8217;); $post-&gt;description = $request-&gt;input(&#8216;description&#8217;); $post-&gt;slug = str_slug($request-&gt;input(&#8216;title&#8217;)); $post-&gt;photo = $path; $post-&gt;save(); [&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-22507","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22507","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=22507"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22507\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}