{"id":49467,"date":"2024-12-02T11:56:11","date_gmt":"2024-12-02T03:56:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49467\/"},"modified":"2024-12-02T11:56:11","modified_gmt":"2024-12-02T03:56:11","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-laravel-%e5%88%9b%e5%bb%ba-rest-api","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49467\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Laravel \u521b\u5efa REST API"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Laravel \u521b\u5efa REST API<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\u7c73\u4e91\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 Laravel \u521b\u5efa REST API\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241202\/1733121393674d557190c5e.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 Laravel \u521b\u5efa REST API\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 Laravel \u521b\u5efa REST API\u63d2\u56fe\" \/><\/p>\n<p>\u60a8\u597d\uff01\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u5728 laravel \u4e2d\u6784\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684 rest api \u6765\u7ba1\u7406\u4efb\u52a1\u3002\u6211\u5c06\u6307\u5bfc\u60a8\u5b8c\u6210\u4ece\u8bbe\u7f6e\u9879\u76ee\u5230\u521b\u5efa\u81ea\u52a8\u5316\u6d4b\u8bd5\u7684\u57fa\u672c\u6b65\u9aa4\u3002<\/p>\n<h3> \u7b2c 1 \u6b65\uff1a\u9879\u76ee\u8bbe\u7f6e <\/h3>\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 laravel \u9879\u76ee\uff1a<\/p>\n<pre>composer create-project laravel\/laravel task-api\ncd task-api\ncode .\n<\/pre>\n<p>\u914d\u7f6e\u6570\u636e\u5e93\uff1a<br \/> \u5728 .env \u6587\u4ef6\u4e2d\uff0c\u8bbe\u7f6e\u6570\u636e\u5e93\u914d\u7f6e\uff1a<\/p>\n<pre>db_database=task_api\ndb_username=your_username\ndb_password=your_password\n<\/pre>\n<p>\u751f\u6210\u4efb\u52a1\u8868\uff1a<br \/> \u8fd0\u884c\u547d\u4ee4\u4e3a\u4efb\u52a1\u8868\u521b\u5efa\u65b0\u7684\u8fc1\u79fb\uff1a<\/p>\n<pre>php artisan make:migration create_tasks_table --create=tasks\n<\/pre>\n<p>\u5728\u8fc1\u79fb\u6587\u4ef6\uff08database\/migrations\/xxxx_xx_xx_create_tasks_table.php\uff09\u4e2d\uff0c\u5b9a\u4e49\u8868\u7ed3\u6784\uff1a<\/p>\n<pre>&lt;?php\n\nuse illuminate\\database\\migrations\\migration;\nuse illuminate\\database\\schema\\blueprint;\nuse illuminate\\support\\facades\\schema;\n\nreturn new class extends migration\n{\n    public function up(): void\n    {\n        schema::create('tasks', function (blueprint $table) {\n            $table-&gt;id();\n            $table-&gt;string('title');\n            $table-&gt;text('description')-&gt;nullable();\n            $table-&gt;boolean('completed')-&gt;default(false);\n            $table-&gt;timestamps();\n        });\n    }\n\n    public function down(): void\n    {\n        schema::dropifexists('tasks');\n    }\n};\n<\/pre>\n<p>\u8fd0\u884c\u8fc1\u79fb\u4ee5\u521b\u5efa\u8868\uff1a<\/p>\n<pre>php artisan migrate\n<\/pre>\n<h3> \u7b2c 2 \u6b65\uff1a\u521b\u5efa\u6a21\u578b\u548c\u63a7\u5236\u5668 <\/h3>\n<p>\u4e3a\u4efb\u52a1\u521b\u5efa\u6a21\u578b\u548c\u63a7\u5236\u5668\uff1a<\/p>\n<pre>php artisan make:model task\nphp artisan make:controller taskcontroller --api\n<\/pre>\n<p>\u5b9a\u4e49\u4efb\u52a1\u6a21\u578b\uff08app\/models\/task.php\uff09\uff1a<\/p>\n<pre>&lt;?php\n\nnamespace app\\models;\n\nuse illuminate\\database\\eloquent\\factories\\hasfactory;\nuse illuminate\\database\\eloquent\\model;\n\nclass task extends model\n{\n    use hasfactory;\n\n    protected $fillable = ['title', 'description', 'completed'];\n}\n<\/pre>\n<h3> \u7b2c 3 \u6b65\uff1a\u5b9a\u4e49 api \u8def\u7531 <\/h3>\n<p>\u5728routes\/api.php\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0taskcontroller\u7684\u8def\u7531\uff1a<\/p>\n<pre>&lt;?php\n\nuse app\\http\\controllers\\taskcontroller;\nuse illuminate\\support\\facades\\route;\n\nroute::apiresource('tasks', taskcontroller::class);\n<\/pre>\n<h3> \u7b2c\u56db\u6b65\uff1a\u5728taskcontroller\u4e2d\u5b9e\u73b0crud <\/h3>\n<p>\u5728taskcontroller\u4e2d\uff0c\u6211\u4eec\u5c06\u5b9e\u73b0\u57fa\u672c\u7684crud\u65b9\u6cd5\u3002<\/p>\n<pre>&lt;?php\n\nnamespace app\\http\\controllers;\n\nuse app\\models\\task;\nuse illuminate\\http\\request;\n\nclass taskcontroller extends controller\n{\n    public function index()\n    {\n        $tasks = task::all();\n        return response()-&gt;json($tasks, 200);\n    }\n\n    public function store(request $request)\n    {\n        $request-&gt;validate([\n            'title' =&gt; 'required|string|max:255',\n            'description' =&gt; 'nullable|string'\n        ]);\n        $task = task::create($request-&gt;all());\n        return response()-&gt;json($task, 201);\n    }\n\n    public function show(task $task)\n    {\n        return response()-&gt;json($task, 200);\n    }\n\n    public function update(request $request, task $task)\n    {\n        $request-&gt;validate([\n            'title' =&gt; 'required|string|max:255',\n            'description' =&gt; 'nullable|string',\n            'completed' =&gt; 'boolean'\n        ]);\n        $task-&gt;update($request-&gt;all());\n        return response()-&gt;json($task, 201);\n    }\n\n    public function destroy(task $task)\n    {\n        $task-&gt;delete();\n        return response()-&gt;json(null, 204);\n    }\n}\n<\/pre>\n<h3> \u6b65\u9aa4 5\uff1a\u6d4b\u8bd5\u7aef\u70b9\uff08vs code\uff09 <\/h3>\n<p>\u73b0\u5728\u6211\u4eec\u5c06\u4f7f\u7528\u540d\u4e3a <strong>rest client<\/strong> \u7684 vs code \u6269\u5c55\u624b\u52a8\u6d4b\u8bd5\u6bcf\u4e2a\u7aef\u70b9 (https:\/\/marketplace.visualstudio.com\/items?itemname=humao.rest-client)\u3002\u5982\u679c\u60a8\u613f\u610f\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<strong>\u5931\u7720<\/strong>\u6216<strong>\u90ae\u9012\u5458<\/strong>\uff01<\/p>\n<p>\u5b89\u88c5\u6269\u5c55\u7a0b\u5e8f\u540e\uff0c\u5728\u9879\u76ee\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\u7684 .http \u6587\u4ef6\uff1a<\/p>\n<pre>### create new task\npost http:\/\/127.0.0.1:8000\/api\/tasks http\/1.1\ncontent-type: application\/json\naccept: application\/json\n\n{\n    \"title\": \"study laravel\"\n}\n\n### show tasks\nget http:\/\/127.0.0.1:8000\/api\/tasks http\/1.1\ncontent-type: application\/json\naccept: application\/json\n\n### show task\nget http:\/\/127.0.0.1:8000\/api\/tasks\/1 http\/1.1\ncontent-type: application\/json\naccept: application\/json\n\n### update task\nput http:\/\/127.0.0.1:8000\/api\/tasks\/1 http\/1.1\ncontent-type: application\/json\naccept: application\/json\n\n{\n    \"title\": \"study laravel and docker\",\n    \"description\": \"we are studying!\",\n    \"completed\": false\n}\n\n### delete task\ndelete http:\/\/127.0.0.1:8000\/api\/tasks\/1 http\/1.1\ncontent-type: application\/json\naccept: application\/json\n<\/pre>\n<p>\u6b64\u6587\u4ef6\u53ef\u8ba9\u60a8\u4f7f\u7528 <strong>rest \u5ba2\u6237\u7aef<\/strong> \u6269\u5c55\u76f4\u63a5\u4ece vs code \u53d1\u9001\u8bf7\u6c42\uff0c\u4ece\u800c\u8f7b\u677e\u6d4b\u8bd5 api \u4e2d\u7684\u6bcf\u4e2a\u8def\u7531\u3002<\/p>\n<h3> \u7b2c 6 \u6b65\uff1a\u6d4b\u8bd5 api <\/h3>\n<p>\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u6d4b\u8bd5\u4ee5\u786e\u4fdd\u6bcf\u6761\u8def\u7ebf\u6309\u9884\u671f\u5de5\u4f5c\u3002<\/p>\n<p>\u9996\u5148\uff0c\u4e3a\u4efb\u52a1\u6a21\u578b\u521b\u5efa\u4e00\u4e2a\u5de5\u5382\uff1a<\/p>\n<pre>php artisan make:factory taskfactory\n<\/pre>\n<pre>&lt;?php\n\nnamespace database\\factories;\n\nuse illuminate\\database\\eloquent\\factories\\factory;\n\nclass taskfactory extends factory\n{\n    public function definition(): array\n    {\n        return [\n            'title' =&gt; fake()-&gt;sentence(),\n            'description' =&gt; fake()-&gt;paragraph(),\n            'completed' =&gt; false,\n        ];\n    }\n}\n<\/pre>\n<p>phpunit \u914d\u7f6e\uff1a<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;phpunit xmlns:xsi=\"http:\/\/www.w3.org\/2001\/xmlschema-instance\"\n    xsi:nonamespaceschemalocation=\"vendor\/phpunit\/phpunit\/phpunit.xsd\"\n    bootstrap=\"vendor\/autoload.php\"\n    colors=\"true\"\n&gt;\n    &lt;testsuites&gt;\n        &lt;testsuite name=\"unit\"&gt;\n            &lt;directory&gt;tests\/unit&lt;\/directory&gt;\n        &lt;\/testsuite&gt;\n        &lt;testsuite name=\"feature\"&gt;\n            &lt;directory&gt;tests\/feature&lt;\/directory&gt;\n        &lt;\/testsuite&gt;\n    &lt;\/testsuites&gt;\n    &lt;source&gt;\n        &lt;include&gt;\n            &lt;directory&gt;app&lt;\/directory&gt;\n        &lt;\/include&gt;\n    &lt;\/source&gt;\n    &lt;php&gt;\n        &lt;env name=\"app_env\" value=\"testing\" \/&gt;\n        &lt;env name=\"bcrypt_rounds\" value=\"4\" \/&gt;\n        &lt;env name=\"cache_driver\" value=\"array\" \/&gt;\n        &lt;env name=\"db_connection\" value=\"sqlite\" \/&gt;\n        &lt;env name=\"db_database\" value=\":memory:\" \/&gt;\n        &lt;env name=\"mail_mailer\" value=\"array\" \/&gt;\n        &lt;env name=\"pulse_enabled\" value=\"false\" \/&gt;\n        &lt;env name=\"queue_connection\" value=\"sync\" \/&gt;\n        &lt;env name=\"session_driver\" value=\"array\" \/&gt;\n        &lt;env name=\"telescope_enabled\" value=\"false\" \/&gt;\n    &lt;\/php&gt;\n&lt;\/phpunit&gt;\n<\/pre>\n<p>\u521b\u5efa\u96c6\u6210\u6d4b\u8bd5\uff1a<\/p>\n<pre>php artisan make:test taskapitest\n<\/pre>\n<p>\u5728tests\/feature\/taskapitest.php\u6587\u4ef6\u4e2d\uff0c\u5b9e\u73b0\u6d4b\u8bd5\uff1a<\/p>\n<pre>&lt;?php\n\nnamespace tests\\feature;\n\nuse app\\models\\task;\nuse illuminate\\foundation\\testing\\refreshdatabase;\nuse tests\\testcase;\n\nclass taskapitest extends testcase\n{\n    use refreshdatabase;\n\n    public function test_can_create_task(): void\n    {\n        $response = $this-&gt;postjson('\/api\/tasks', [\n            'title' =&gt; 'new task',\n            'description' =&gt; 'task description',\n            'completed' =&gt; false,\n        ]);\n\n        $response-&gt;assertstatus(201);\n\n        $response-&gt;assertjson([\n            'title' =&gt; 'new task',\n            'description' =&gt; 'task description',\n            'completed' =&gt; false,\n        ]);\n    }\n\n    public function test_can_list_tasks()\n    {\n        task::factory()-&gt;count(3)-&gt;create();\n\n        $response = $this-&gt;getjson('\/api\/tasks');\n\n        $response-&gt;assertstatus(200);\n\n        $response-&gt;assertjsoncount(3);\n    }\n\n    public function test_can_show_task()\n    {\n        $task = task::factory()-&gt;create();\n\n        $response = $this-&gt;getjson(\"\/api\/tasks\/{$task-&gt;id}\");\n\n        $response-&gt;assertstatus(200);\n\n        $response-&gt;assertjson([\n            'title' =&gt; $task-&gt;title,\n            'description' =&gt; $task-&gt;description,\n            'completed' =&gt; false,\n        ]);\n    }\n\n    public function test_can_update_task()\n    {\n        $task = task::factory()-&gt;create();\n\n        $response = $this-&gt;putjson(\"\/api\/tasks\/{$task-&gt;id}\", [\n            'title' =&gt; 'update task',\n            'description' =&gt; 'update description',\n            'completed' =&gt; true,\n        ]);\n\n        $response-&gt;assertstatus(201);\n\n        $response-&gt;assertjson([\n            'title' =&gt; 'update task',\n            'description' =&gt; 'update description',\n            'completed' =&gt; true,\n        ]);\n    }\n\n    public function test_can_delete_task()\n    {\n        $task = task::factory()-&gt;create();\n\n        $response = $this-&gt;deletejson(\"\/api\/tasks\/{$task-&gt;id}\");\n\n        $response-&gt;assertstatus(204);\n\n        $this-&gt;assertdatabasemissing('tasks', ['id' =&gt; $task-&gt;id]);\n    }\n}\n<\/pre>\n<p><strong>\u8fd0\u884c\u6d4b\u8bd5<\/strong>\uff1a<\/p>\n<pre>php artisan test\n<\/pre>\n<p>*<em>\u8c22\u8c22\uff01 *<\/em><\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 Laravel \u521b\u5efa REST API\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Laravel \u521b\u5efa REST API \u6536\u85cf \u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\u7c73\u4e91\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u300a\u5982\u4f55\u4f7f\u7528 Laravel \u521b\u5efa REST API\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01 \u60a8\u597d\uff01\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u5728 laravel \u4e2d\u6784\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684 rest api \u6765\u7ba1\u7406\u4efb\u52a1\u3002\u6211\u5c06\u6307\u5bfc\u60a8\u5b8c\u6210\u4ece\u8bbe\u7f6e\u9879\u76ee\u5230\u521b\u5efa\u81ea\u52a8\u5316\u6d4b\u8bd5\u7684\u57fa\u672c\u6b65\u9aa4\u3002 \u7b2c 1 \u6b65\uff1a\u9879\u76ee\u8bbe\u7f6e \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 laravel \u9879\u76ee\uff1a composer create-project laravel\/laravel task-api cd task-api code . \u914d\u7f6e\u6570\u636e\u5e93\uff1a \u5728 .env \u6587\u4ef6\u4e2d\uff0c\u8bbe\u7f6e\u6570\u636e\u5e93\u914d\u7f6e\uff1a db_database=task_api db_username=your_username db_password=your_password \u751f\u6210\u4efb\u52a1\u8868\uff1a \u8fd0\u884c\u547d\u4ee4\u4e3a\u4efb\u52a1\u8868\u521b\u5efa\u65b0\u7684\u8fc1\u79fb\uff1a php artisan make:migration create_tasks_table &#8211;create=tasks \u5728\u8fc1\u79fb\u6587\u4ef6\uff08database\/migrations\/xxxx_xx_xx_create_tasks_table.php\uff09\u4e2d\uff0c\u5b9a\u4e49\u8868\u7ed3\u6784\uff1a &lt;?php use illuminate\\database\\migrations\\migration; use illuminate\\database\\schema\\blueprint; use illuminate\\support\\facades\\schema; return new [&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-49467","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49467","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=49467"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49467\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}