{"id":22581,"date":"2024-11-21T12:43:05","date_gmt":"2024-11-21T04:43:05","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22581\/"},"modified":"2024-11-21T12:43:05","modified_gmt":"2024-11-21T04:43:05","slug":"laravel-%e5%a2%9e%e5%8a%a0%e8%b7%af%e7%94%b1%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22581\/","title":{"rendered":"laravel \u589e\u52a0\u8def\u7531\u6587\u4ef6"},"content":{"rendered":"<p>laravel\u662f\u4e00\u4e2a\u6d41\u884c\u7684php\u6846\u67b6\uff0c\u5177\u6709\u7b80\u6d01\u4f18\u7f8e\u7684\u8bed\u6cd5\u548c\u5f3a\u5927\u7684\u529f\u80fd\u3002\u5728\u4f7f\u7528laravel\u5f00\u53d1\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u6dfb\u52a0\u81ea\u5b9a\u4e49\u8def\u7531\u6765\u5904\u7406\u8bbf\u95ee\u4e0d\u540c\u9875\u9762\u7684\u8bf7\u6c42\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728laravel\u9879\u76ee\u4e2d\u589e\u52a0\u81ea\u5b9a\u4e49\u8def\u7531\u6587\u4ef6\uff0c\u65b9\u4fbf\u5f00\u53d1\u4eba\u5458\u7ba1\u7406\u548c\u7ef4\u62a4\u8def\u7531\u4fe1\u606f\u3002<\/p>\n<ol>\n<li>\u65b0\u5efa\u8def\u7531\u6587\u4ef6<\/li>\n<\/ol>\n<p>\u5728Laravel\u9879\u76ee\u7684routes\u76ee\u5f55\u4e0b\uff0c\u901a\u5e38\u4f1a\u6709\u4e00\u4e2aweb.php\u6587\u4ef6\u548capi.php\u6587\u4ef6\u3002\u8fd9\u4e24\u4e2a\u6587\u4ef6\u5206\u522b\u7528\u4e8e\u5b9a\u4e49Web\u548cAPI\u8def\u7531\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2a\u540d\u4e3acustom.php\u7684\u6587\u4ef6\uff0c\u7528\u4e8e\u5b58\u653e\u81ea\u5b9a\u4e49\u8def\u7531\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5728\u547d\u4ee4\u884c\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\uff1a<\/p>\n<pre>touch routes\/custom.php<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u914d\u7f6e\u8def\u7531\u6587\u4ef6<\/li>\n<\/ol>\n<p>\u6253\u5f00custom.php\u6587\u4ef6\uff0c\u5f00\u59cb\u914d\u7f6e\u81ea\u5b9a\u4e49\u8def\u7531\u3002\u8def\u7531\u5206\u4e3aget\u3001post\u3001put\u3001delete\u7b49\u7c7b\u578b\uff0c\u5206\u522b\u5904\u7406\u7528\u6237\u7684\u8bf7\u6c42\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u7528\u4e8e\u5904\u7406GET\u8bf7\u6c42\u8bbf\u95ee\u4e00\u4e2a\u81ea\u5b9a\u4e49\u9875\u9762\uff1a<\/p>\n<pre>Route::get('\/custom-page', function () {\n    return view('custom');\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3acustom-page\u7684\u8def\u7531\uff0c\u8be5\u8def\u7531\u7684\u54cd\u5e94\u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a\u540d\u4e3acustom\u7684\u89c6\u56fe\uff0c\u7528\u4e8e\u5c55\u793a\u81ea\u5b9a\u4e49\u9875\u9762\u3002\u8fd9\u91cc\u7684\u89c6\u56fe\u6587\u4ef6\u9700\u8981\u5728resources\/views\u76ee\u5f55\u4e0b\u5148\u521b\u5efa\u597d\u3002<\/p>\n<ol>\n<li>\u6ce8\u518c\u8def\u7531\u6587\u4ef6<\/li>\n<\/ol>\n<p>\u4e3a\u4e86\u8ba9Laravel\u9879\u76ee\u80fd\u591f\u8bc6\u522b\u548c\u4f7f\u7528custom.php\u6587\u4ef6\u4e2d\u7684\u8def\u7531\uff0c\u9700\u8981\u5728\u9879\u76ee\u4e2d\u6ce8\u518c\u8be5\u6587\u4ef6\u3002\u53ef\u4ee5\u5728app\/Providers\/RouteServiceProvider.php\u6587\u4ef6\u4e2d\u7684map\u51fd\u6570\u4e2d\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>protected function mapCustomRoutes()\n{\n    Route::middleware('web')\n        -&amp;gt;namespace($this-&amp;gt;namespace)\n        -&amp;gt;group(base_path('routes\/custom.php'));\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3amapCustomRoutes\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u6ce8\u518c\u81ea\u5b9a\u4e49\u8def\u7531\u6587\u4ef6\u3002\u5728\u8be5\u51fd\u6570\u4e2d\u4f7f\u7528group\u51fd\u6570\u5c06\u81ea\u5b9a\u4e49\u8def\u7531\u6587\u4ef6\u7ed1\u5b9a\u5230Web\u4e2d\u95f4\u4ef6\u4e0b\uff0c\u5e76\u6307\u5b9a\u4e86\u8def\u7531\u6587\u4ef6\u6240\u5904\u7684\u76ee\u5f55\u8def\u5f84\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u5728RouteServiceProvider.php\u6587\u4ef6\u4e2d\u7684map\u51fd\u6570\u4e2d\u8c03\u7528mapCustomRoutes\u51fd\u6570\u5373\u53ef\u5b8c\u6210\u81ea\u5b9a\u4e49\u8def\u7531\u7684\u6ce8\u518c\uff1a<\/p>\n<pre>public function map()\n{\n    $this-&amp;gt;mapWebRoutes();\n\n    $this-&amp;gt;mapApiRoutes();\n\n    $this-&amp;gt;mapCustomRoutes(); \/\/ \u6ce8\u518c\u81ea\u5b9a\u4e49\u8def\u7531\u6587\u4ef6\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u6d4b\u8bd5\u81ea\u5b9a\u4e49\u8def\u7531<\/li>\n<\/ol>\n<p>\u5b8c\u6210\u81ea\u5b9a\u4e49\u8def\u7531\u6587\u4ef6\u7684\u914d\u7f6e\u548c\u6ce8\u518c\u540e\uff0c\u5c31\u53ef\u4ee5\u5728\u9879\u76ee\u4e2d\u6d4b\u8bd5\u81ea\u5b9a\u4e49\u8def\u7531\u4e86\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8Laravel\u9879\u76ee\uff0c\u5e76\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95eehttp:\/\/localhost\/custom-page\uff0c\u67e5\u770b\u662f\u5426\u6210\u529f\u52a0\u8f7d\u81ea\u5b9a\u4e49\u9875\u9762\uff1a<\/p>\n<pre>php artisan serve<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u81ea\u5b9a\u4e49\u9875\u9762\u7684\u8bbf\u95ee\u5730\u5740\u4e3a\uff1ahttp:\/\/localhost\/custom-page<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728Laravel\u9879\u76ee\u4e2d\u589e\u52a0\u81ea\u5b9a\u4e49\u8def\u7531\u6587\u4ef6\u7684\u6b65\u9aa4\u3002\u901a\u8fc7\u521b\u5efa\u81ea\u5b9a\u4e49\u8def\u7531\u6587\u4ef6\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u548c\u7ef4\u62a4Laravel\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f17\u591a\u7684\u8def\u7531\u4fe1\u606f\u3002\u540c\u65f6\uff0c\u4e5f\u4f7f\u5f97\u8def\u7531\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u5f00\u53d1\u4eba\u5458\u7406\u89e3\u548c\u8c03\u8bd5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel \u589e\u52a0\u8def\u7531\u6587\u4ef6\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\u662f\u4e00\u4e2a\u6d41\u884c\u7684php\u6846\u67b6\uff0c\u5177\u6709\u7b80\u6d01\u4f18\u7f8e\u7684\u8bed\u6cd5\u548c\u5f3a\u5927\u7684\u529f\u80fd\u3002\u5728\u4f7f\u7528laravel\u5f00\u53d1\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u6dfb\u52a0\u81ea\u5b9a\u4e49\u8def\u7531\u6765\u5904\u7406\u8bbf\u95ee\u4e0d\u540c\u9875\u9762\u7684\u8bf7\u6c42\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728laravel\u9879\u76ee\u4e2d\u589e\u52a0\u81ea\u5b9a\u4e49\u8def\u7531\u6587\u4ef6\uff0c\u65b9\u4fbf\u5f00\u53d1\u4eba\u5458\u7ba1\u7406\u548c\u7ef4\u62a4\u8def\u7531\u4fe1\u606f\u3002 \u65b0\u5efa\u8def\u7531\u6587\u4ef6 \u5728Laravel\u9879\u76ee\u7684routes\u76ee\u5f55\u4e0b\uff0c\u901a\u5e38\u4f1a\u6709\u4e00\u4e2aweb.php\u6587\u4ef6\u548capi.php\u6587\u4ef6\u3002\u8fd9\u4e24\u4e2a\u6587\u4ef6\u5206\u522b\u7528\u4e8e\u5b9a\u4e49Web\u548cAPI\u8def\u7531\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2a\u540d\u4e3acustom.php\u7684\u6587\u4ef6\uff0c\u7528\u4e8e\u5b58\u653e\u81ea\u5b9a\u4e49\u8def\u7531\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5728\u547d\u4ee4\u884c\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\uff1a touch routes\/custom.php \u767b\u5f55\u540e\u590d\u5236 \u914d\u7f6e\u8def\u7531\u6587\u4ef6 \u6253\u5f00custom.php\u6587\u4ef6\uff0c\u5f00\u59cb\u914d\u7f6e\u81ea\u5b9a\u4e49\u8def\u7531\u3002\u8def\u7531\u5206\u4e3aget\u3001post\u3001put\u3001delete\u7b49\u7c7b\u578b\uff0c\u5206\u522b\u5904\u7406\u7528\u6237\u7684\u8bf7\u6c42\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u7528\u4e8e\u5904\u7406GET\u8bf7\u6c42\u8bbf\u95ee\u4e00\u4e2a\u81ea\u5b9a\u4e49\u9875\u9762\uff1a Route::get(&#8216;\/custom-page&#8217;, function () { return view(&#8216;custom&#8217;); }); \u767b\u5f55\u540e\u590d\u5236 \u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3acustom-page\u7684\u8def\u7531\uff0c\u8be5\u8def\u7531\u7684\u54cd\u5e94\u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a\u540d\u4e3acustom\u7684\u89c6\u56fe\uff0c\u7528\u4e8e\u5c55\u793a\u81ea\u5b9a\u4e49\u9875\u9762\u3002\u8fd9\u91cc\u7684\u89c6\u56fe\u6587\u4ef6\u9700\u8981\u5728resources\/views\u76ee\u5f55\u4e0b\u5148\u521b\u5efa\u597d\u3002 \u6ce8\u518c\u8def\u7531\u6587\u4ef6 \u4e3a\u4e86\u8ba9Laravel\u9879\u76ee\u80fd\u591f\u8bc6\u522b\u548c\u4f7f\u7528custom.php\u6587\u4ef6\u4e2d\u7684\u8def\u7531\uff0c\u9700\u8981\u5728\u9879\u76ee\u4e2d\u6ce8\u518c\u8be5\u6587\u4ef6\u3002\u53ef\u4ee5\u5728app\/Providers\/RouteServiceProvider.php\u6587\u4ef6\u4e2d\u7684map\u51fd\u6570\u4e2d\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a protected function mapCustomRoutes() { Route::middleware(&#8216;web&#8217;) -&amp;gt;namespace($this-&amp;gt;namespace) -&amp;gt;group(base_path(&#8216;routes\/custom.php&#8217;)); } \u767b\u5f55\u540e\u590d\u5236 \u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3amapCustomRoutes\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u6ce8\u518c\u81ea\u5b9a\u4e49\u8def\u7531\u6587\u4ef6\u3002\u5728\u8be5\u51fd\u6570\u4e2d\u4f7f\u7528group\u51fd\u6570\u5c06\u81ea\u5b9a\u4e49\u8def\u7531\u6587\u4ef6\u7ed1\u5b9a\u5230Web\u4e2d\u95f4\u4ef6\u4e0b\uff0c\u5e76\u6307\u5b9a\u4e86\u8def\u7531\u6587\u4ef6\u6240\u5904\u7684\u76ee\u5f55\u8def\u5f84\u3002 \u63a5\u4e0b\u6765\uff0c\u5728RouteServiceProvider.php\u6587\u4ef6\u4e2d\u7684map\u51fd\u6570\u4e2d\u8c03\u7528mapCustomRoutes\u51fd\u6570\u5373\u53ef\u5b8c\u6210\u81ea\u5b9a\u4e49\u8def\u7531\u7684\u6ce8\u518c\uff1a public function map() { $this-&amp;gt;mapWebRoutes(); $this-&amp;gt;mapApiRoutes(); $this-&amp;gt;mapCustomRoutes(); \/\/ \u6ce8\u518c\u81ea\u5b9a\u4e49\u8def\u7531\u6587\u4ef6 } \u767b\u5f55\u540e\u590d\u5236 \u6d4b\u8bd5\u81ea\u5b9a\u4e49\u8def\u7531 \u5b8c\u6210\u81ea\u5b9a\u4e49\u8def\u7531\u6587\u4ef6\u7684\u914d\u7f6e\u548c\u6ce8\u518c\u540e\uff0c\u5c31\u53ef\u4ee5\u5728\u9879\u76ee\u4e2d\u6d4b\u8bd5\u81ea\u5b9a\u4e49\u8def\u7531\u4e86\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8Laravel\u9879\u76ee\uff0c\u5e76\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95eehttp:\/\/localhost\/custom-page\uff0c\u67e5\u770b\u662f\u5426\u6210\u529f\u52a0\u8f7d\u81ea\u5b9a\u4e49\u9875\u9762\uff1a php artisan serve \u767b\u5f55\u540e\u590d\u5236 \u81ea\u5b9a\u4e49\u9875\u9762\u7684\u8bbf\u95ee\u5730\u5740\u4e3a\uff1ahttp:\/\/localhost\/custom-page \u603b\u7ed3 \u4ee5\u4e0a\u5c31\u662f\u5728Laravel\u9879\u76ee\u4e2d\u589e\u52a0\u81ea\u5b9a\u4e49\u8def\u7531\u6587\u4ef6\u7684\u6b65\u9aa4\u3002\u901a\u8fc7\u521b\u5efa\u81ea\u5b9a\u4e49\u8def\u7531\u6587\u4ef6\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u548c\u7ef4\u62a4Laravel\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f17\u591a\u7684\u8def\u7531\u4fe1\u606f\u3002\u540c\u65f6\uff0c\u4e5f\u4f7f\u5f97\u8def\u7531\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u5f00\u53d1\u4eba\u5458\u7406\u89e3\u548c\u8c03\u8bd5\u3002 \u4ee5\u4e0a\u5c31\u662flaravel \u589e\u52a0\u8def\u7531\u6587\u4ef6\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/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-22581","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22581","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=22581"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22581\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}