{"id":22390,"date":"2024-11-21T14:29:04","date_gmt":"2024-11-21T06:29:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22390\/"},"modified":"2024-11-21T14:29:04","modified_gmt":"2024-11-21T06:29:04","slug":"laravel%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e9%98%b2%e6%ad%a2%e8%a2%ab%e4%b8%8b%e8%bd%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22390\/","title":{"rendered":"laravel\u5982\u4f55\u5b9e\u73b0\u9632\u6b62\u88ab\u4e0b\u8f7d"},"content":{"rendered":"<p>\u5728\u4fe1\u606f\u65f6\u4ee3\uff0c\u4e0d\u53ef\u907f\u514d\u5730\u5b58\u5728\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u4fdd\u62a4\u7684\u60c5\u51b5\u3002\u5bf9\u4e8eweb\u5e94\u7528\u7a0b\u5e8f\u800c\u8a00\uff0c\u5176\u4e2d\u4e00\u9879\u57fa\u672c\u7684\u5b89\u5168\u63aa\u65bd\u5c31\u662f\u9632\u6b62\u7528\u6237\u6216\u8005\u975e\u6cd5\u7a0b\u5e8f\u4e0b\u8f7d\u6307\u5b9a\u7684\u6587\u4ef6\u3002<\/p>\n<p>\u5728Laravel\u6846\u67b6\u4e2d\uff0c\u60f3\u8981\u9632\u6b62\u6587\u4ef6\u88ab\u4e0b\u8f7d\u7684\u65b9\u6cd5\u6bd4\u8f83\u7b80\u5355\u3002\u672c\u6587\u5c06\u4f1a\u4ecb\u7ecd\u51e0\u4e2a\u59a5\u5584\u4fdd\u62a4\u6587\u4ef6\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u4f7f\u7f51\u7ad9\u66f4\u52a0\u5b89\u5168\uff0c\u5e76\u4e14\u907f\u514d\u88ab\u9ed1\u5ba2\u653b\u51fb\u3002<\/p>\n<p>\u4e00\u3001\u4f7f\u7528Laravel\u7684Routing\u65b9\u6cd5<\/p>\n<p>\u5728Laravel\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Routing\u6765\u63a7\u5236\u54ea\u4e9b\u6587\u4ef6\u53ef\u4ee5\u88ab\u4e0b\u8f7d\uff0c\u54ea\u4e9b\u4e0d\u80fd\u88ab\u4e0b\u8f7d\u3002<\/p>\n<p>Step 1 \u2013 \u521b\u5efaController<\/p>\n<p>\u5728app\/Http\/Controllers\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u63a7\u5236\u5668\u3002\u6211\u4eec\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>php artisan make:controller DownloadController<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5b83\u4f1a\u5728Controllers\u6587\u4ef6\u5939\u4e2d\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u63a7\u5236\u5668\uff1a<\/p>\n<pre>&lt;?php\nnamespace AppHttpControllers;\n\nclass DownloadController extends Controller\n{\n    \/\/\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>Step 2 \u2013 \u521b\u5efaRoute<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u7528\u4ee5\u4e0b\u8def\u7531\u8c03\u7528DownloadController\u4e2d\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre>Route::post('\/download\/{file_name}', 'DownloadController@downloadFile')-&gt;name('download.file');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>Step 3 \u2013 \u521b\u5efaDownload\u65b9\u6cd5<\/p>\n<p>\u4e0b\u4e00\u6b65\u662f\u4e3aDownloadController\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u4eceGET\u8def\u7531\u4e2d\u8c03\u7528\u8be5\u65b9\u6cd5\u3002<\/p>\n<pre>&lt;?php\nnamespace AppHttpControllers;\n\nuse IlluminateHttpRequest;\nuse IlluminateSupportFacadesStorage;\nuse IlluminateSupportFacadesResponse;\n\nclass DownloadController extends Controller\n{\n    public function downloadFile($fileName)\n    {\n        $file = Storage::disk('public')-&gt;get($fileName);\n\n        return Response::download($file, $fileName);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u53ef\u4ee5\u5728Web\u6d4f\u89c8\u5668\u4e2d\u8f93\u5165\u4ee5\u4e0bURL\u5730\u5740\uff0c\u6d4b\u8bd5\u8be5\u7a0b\u5e8f\uff1a<\/p>\n<pre>http:\/\/127.0.0.1:8000\/download\/before-download-test-file.txt<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e8c\u3001\u4f7f\u7528File\u7c7b\u7684\u65b9\u6cd5<\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u7528\u7684\u662fPHP\u7684file_get_contents()\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5177\u6709\u8bbf\u95ee\u6587\u4ef6\u548c\u8bfb\u53d6\u5176\u5185\u5bb9\u7684\u529f\u80fd\u3002<\/p>\n<pre>Route::get('\/download', function(){\n    $file = public_path().\"\/test.zip\";\n    $headers = array(\n              'Content-Type: application\/octet-stream',\n            );\n    return Response::download($file, 'test.zip', $headers);\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e09\u3001\u76f4\u63a5\u8bfb\u53d6\u6587\u4ef6<\/p>\n<p>\u7b2c\u4e09\u79cd\u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5176\u57fa\u672c\u601d\u8def\u662f\u5c06\u6587\u4ef6\u76f4\u63a5\u8bfb\u5165\u5185\u5b58\u5e76\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<pre>Route::get('\/download', function(){\n    $file = public_path().\"\/test.zip\";\n    $headers = array(\n              'Content-Type: application\/octet-stream',\n            );\n    return Response::make(file_get_contents($file), 200, $headers);\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5c0f\u7ed3<\/p>\n<p>\u5173\u4e8e\u5982\u4f55\u9632\u6b62Laravel\u4e2d\u7684\u6587\u4ef6\u88ab\u4e0b\u8f7d\uff0c\u6709\u51e0\u79cd\u53e3\u5f84\u53ef\u4ee5\u9009\u62e9\u3002\u6700\u521d\uff0c\u53ef\u4ee5\u4f7f\u7528Routing\u6765\u63a7\u5236\u54ea\u4e9b\u6587\u4ef6\u53ef\u4ee5\u88ab\u4e0b\u8f7d\uff0c\u54ea\u4e9b\u4e0d\u80fd\u88ab\u4e0b\u8f7d\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u6709\u4e24\u79cd\u6807\u51c6\u65b9\u6cd5\uff1a\u4f7f\u7528Laravel\u7684File\u7c7b\uff08file_get_contents()\uff09\u6216\u76f4\u63a5\u8bfb\u53d6\u6587\u4ef6\u3002\u5bf9\u4e8e\u6587\u4ef6\u4fdd\u62a4\u548c\u4e0b\u8f7d\u5b89\u5168\uff0c\u4fdd\u8bc1\u7f51\u7ad9\u5b89\u5168\u81f3\u5173\u91cd\u8981\uff0c\u5982\u679c\u6b63\u786e\u5b9e\u65bd\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u589e\u5f3a\u7f51\u7ad9\u7684\u5b89\u5168\u6027\uff0c\u907f\u514d\u88ab\u865a\u5047\u653b\u51fb\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel\u5982\u4f55\u5b9e\u73b0\u9632\u6b62\u88ab\u4e0b\u8f7d\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>\u5728\u4fe1\u606f\u65f6\u4ee3\uff0c\u4e0d\u53ef\u907f\u514d\u5730\u5b58\u5728\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u4fdd\u62a4\u7684\u60c5\u51b5\u3002\u5bf9\u4e8eweb\u5e94\u7528\u7a0b\u5e8f\u800c\u8a00\uff0c\u5176\u4e2d\u4e00\u9879\u57fa\u672c\u7684\u5b89\u5168\u63aa\u65bd\u5c31\u662f\u9632\u6b62\u7528\u6237\u6216\u8005\u975e\u6cd5\u7a0b\u5e8f\u4e0b\u8f7d\u6307\u5b9a\u7684\u6587\u4ef6\u3002 \u5728Laravel\u6846\u67b6\u4e2d\uff0c\u60f3\u8981\u9632\u6b62\u6587\u4ef6\u88ab\u4e0b\u8f7d\u7684\u65b9\u6cd5\u6bd4\u8f83\u7b80\u5355\u3002\u672c\u6587\u5c06\u4f1a\u4ecb\u7ecd\u51e0\u4e2a\u59a5\u5584\u4fdd\u62a4\u6587\u4ef6\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u4f7f\u7f51\u7ad9\u66f4\u52a0\u5b89\u5168\uff0c\u5e76\u4e14\u907f\u514d\u88ab\u9ed1\u5ba2\u653b\u51fb\u3002 \u4e00\u3001\u4f7f\u7528Laravel\u7684Routing\u65b9\u6cd5 \u5728Laravel\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Routing\u6765\u63a7\u5236\u54ea\u4e9b\u6587\u4ef6\u53ef\u4ee5\u88ab\u4e0b\u8f7d\uff0c\u54ea\u4e9b\u4e0d\u80fd\u88ab\u4e0b\u8f7d\u3002 Step 1 \u2013 \u521b\u5efaController \u5728app\/Http\/Controllers\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u63a7\u5236\u5668\u3002\u6211\u4eec\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a php artisan make:controller DownloadController \u767b\u5f55\u540e\u590d\u5236 \u5b83\u4f1a\u5728Controllers\u6587\u4ef6\u5939\u4e2d\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u63a7\u5236\u5668\uff1a &lt;?php namespace AppHttpControllers; class DownloadController extends Controller { \/\/ } \u767b\u5f55\u540e\u590d\u5236 Step 2 \u2013 \u521b\u5efaRoute \u6211\u4eec\u53ef\u4ee5\u7528\u4ee5\u4e0b\u8def\u7531\u8c03\u7528DownloadController\u4e2d\u7684\u65b9\u6cd5\uff1a Route::post(&#8216;\/download\/{file_name}&#8217;, &#8216;DownloadController@downloadFile&#8217;)-&gt;name(&#8216;download.file&#8217;); \u767b\u5f55\u540e\u590d\u5236 Step 3 \u2013 \u521b\u5efaDownload\u65b9\u6cd5 \u4e0b\u4e00\u6b65\u662f\u4e3aDownloadController\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u4eceGET\u8def\u7531\u4e2d\u8c03\u7528\u8be5\u65b9\u6cd5\u3002 &lt;?php namespace AppHttpControllers; use IlluminateHttpRequest; use IlluminateSupportFacadesStorage; use IlluminateSupportFacadesResponse; class DownloadController extends Controller { public function [&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-22390","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22390","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=22390"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22390\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}