{"id":22366,"date":"2024-11-21T12:38:48","date_gmt":"2024-11-21T04:38:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22366\/"},"modified":"2024-11-21T12:38:48","modified_gmt":"2024-11-21T04:38:48","slug":"laravel%e9%a1%b9%e7%9b%ae%e4%b8%ad%e5%a6%82%e4%bd%95%e7%bb%91%e5%ae%9a%e8%b7%af%e7%94%b1","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22366\/","title":{"rendered":"laravel\u9879\u76ee\u4e2d\u5982\u4f55\u7ed1\u5b9a\u8def\u7531"},"content":{"rendered":"<p>laravel\u662f\u4e00\u4e2a\u4f7f\u7528php\u8bed\u8a00\u7f16\u5199\u7684\u5f00\u6e90\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u5feb\u901f\u5f00\u53d1\u9ad8\u54c1\u8d28\u7684web\u5e94\u7528\u7a0b\u5e8f\u3002\u5728laravel\u6846\u67b6\u4e2d\uff0c\u8def\u7531\u662f\u5c06url\uff08uniform resource locator\uff09\u6620\u5c04\u5230\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7ed9\u5b9a\u7684\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u5728Laravel\u4e2d\uff0c\u5c06URL\u548c\u5e94\u7528\u7a0b\u5e8f\u64cd\u4f5c\u6620\u5c04\u7684\u8fc7\u7a0b\u79f0\u4e3a\u8def\u7531\u7ed1\u5b9a\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5728Laravel\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5982\u4f55\u7ed1\u5b9a\u8def\u7531\u3002<\/p>\n<p>\u4e00\u3001\u57fa\u672c\u8def\u7531\u7ed1\u5b9a<\/p>\n<p>\u5728Laravel\u4e2d\u5b9a\u4e49\u8def\u7531\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Route facade\u63d0\u4f9b\u7684\u65b9\u6cd5\u3002Route::get(), Route::post()\uff0cRoute::put()\u548cRoute::delete()\u7b49\u65b9\u6cd5\u8def\u7531\u7684HTTP\u8bf7\u6c42\u7c7b\u578b\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u8def\u7531\uff1a<\/p>\n<pre>Route::get('\/', function () {\n    return 'Hello World';\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u57fa\u672c\u8def\u7531\uff0c\u7528\u4e8e\u5904\u7406\u6839URL\uff08\/\uff09\u7684HTTP GET\u8bf7\u6c42\u3002\u5f53\u7528\u6237\u8bbf\u95ee\u6839URL\u65f6\uff0cLaravel\u5c06\u8fd4\u56de\u4e00\u4e2a\u201cHello World\u201d\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4e8c\u3001\u8def\u7531\u53c2\u6570\u7ed1\u5b9a<\/p>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u5728Laravel\u4e2d\u7ed1\u5b9a\u5e26\u53c2\u6570\u7684\u8def\u7531\u3002<\/p>\n<p>\u6211\u4eec\u4f7f\u7528{parameter}\u6765\u8868\u793a\u4e00\u4e2a\u8def\u7531\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5e26\u6709URL\u53c2\u6570\u7684\u8def\u7531\u3002<\/p>\n<pre>Route::get('\/hello\/{name}', function ($name) {\n    return 'Hello ' . $name;\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a\u201cname\u201d\u7684URL\u53c2\u6570\u3002\u5f53\u7528\u6237\u8bbf\u95ee\/hello\/{name} URL\u65f6\uff0cLaravel\u5c06\u628a\u53c2\u6570\u540d\u79f0\u6620\u5c04\u5230\u4f20\u9012\u7ed9\u5904\u7406\u7a0b\u5e8f\u7684\u53c2\u6570\u4e2d\u3002<\/p>\n<p>\u4e09\u3001\u547d\u540d\u8def\u7531\u7ed1\u5b9a<\/p>\n<p>\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u4e00\u4e2a\u7279\u5b9a\u7684\u8def\u7531\u5206\u914d\u4e00\u4e2a\u540d\u79f0\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u547d\u540d\u8def\u7531\u7ed1\u5b9a\u6765\u4e3a\u8def\u7531\u5206\u914d\u540d\u79f0\uff0c\u8fd9\u5728\u91cd\u6784\u4ee3\u7801\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u4e3a\u8def\u7531\u5206\u914d\u540d\u79f0\uff1a<\/p>\n<pre>Route::get('\/about', function () {\n    return 'This is the about page';\n})-&gt;name('about');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528name()\u65b9\u6cd5\u5c06\u8def\u7531\u5206\u914d\u4e00\u4e2a\u540d\u79f0\uff0c\u540d\u79f0\u4e3a\u201cabout\u201d\u3002<\/p>\n<p>\u56db\u3001\u8def\u7531\u5206\u7ec4<\/p>\n<p>\u8def\u7531\u5206\u7ec4\u662f\u58f0\u660e\u5728\u540c\u4e00\u5206\u7ec4\u4e2d\u7684\u591a\u4e2a\u8def\u7531\u7684\u6280\u672f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5206\u7ec4\u5355\u72ec\u5b9a\u4e49\u8def\u7531\u5668\u7684\u5904\u7406\u7a0b\u5e8f\uff0c\u4e2d\u95f4\u4ef6\u6216\u524d\u7f00\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7528\u4e8e\u8def\u7531\u5206\u7ec4\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>Route::prefix('admin')-&gt;group(function () {\n    Route::get('dashboard', function () {\n        return 'This is the admin dashboard';\n    });\n\n    Route::get('users', function () {\n        return 'This is the admin users page';\n    });\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528prefix()\u65b9\u6cd5\u4e3a\u8def\u7531\u5668\u6dfb\u52a0\u524d\u7f00\u3002\u7136\u540e\u6211\u4eec\u5c06\u4e24\u4e2a\u8def\u7531\u5305\u542b\u5728\u4e00\u4e2a\u5206\u7ec4\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2aURI\uff08\/admin\/dashboard\uff09\u548c\u4e00\u4e2a\u7528\u4e8e\u7528\u6237\u6570\u636e\u7684URI\uff08\/admin\/users\uff09\u3002<\/p>\n<p>\u4e94\u3001\u8def\u7531\u4e2d\u4f7f\u7528\u63a7\u5236\u5668<\/p>\n<p>\u5728Laravel\u4e2d\uff0c\u63a7\u5236\u5668\u662f\u4e00\u4e2a\u7c7b\uff0c\u5b83\u8d1f\u8d23\u5904\u7406\u8bf7\u6c42\u5e76\u8fd4\u56de\u54cd\u5e94\u3002\u4f7f\u7528\u63a7\u5236\u5668\u53ef\u4ee5\u5c06\u4ee3\u7801\u903b\u8f91\u4ece\u8def\u7531\u6587\u4ef6\u4e2d\u5206\u79bb\u51fa\u6765\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5728Laravel\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u63a7\u5236\u5668\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>Route::get('\/hello\/{name}', 'HelloController@index');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8def\u7531\uff0c\u5f53\u7528\u6237\u8bbf\u95eeURL\/hello\/{name}\u65f6\uff0c\u5c06\u8c03\u7528HelloController\u63a7\u5236\u5668\u7684index()\u65b9\u6cd5\u3002\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c$name\u53c2\u6570\u5c06\u4f5c\u4e3a\u63a7\u5236\u5668\u65b9\u6cd5\u7684\u53c2\u6570\u4f20\u9012\u3002<\/p>\n<p>\u516d\u3001\u8def\u7531\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6<\/p>\n<p>\u5728Laravel\u4e2d\uff0c\u4e2d\u95f4\u4ef6\u662f\u5728\u8bf7\u6c42\u548c\u54cd\u5e94\u4e4b\u95f4\u6267\u884c\u7684\u7ec4\u4ef6\u3002\u4e2d\u95f4\u4ef6\u53ef\u4ee5\u7528\u4e8e\u9a8c\u8bc1\u8bf7\u6c42\uff0c\u68c0\u67e5\u7528\u6237\u662f\u5426\u5df2\u767b\u5f55\uff0c\u63a7\u5236\u8bbf\u95ee\u7b49\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5728Laravel\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>Route::get('\/dashboard', function () {\n    return 'This is the dashboard page';\n})-&gt;middleware('auth');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8def\u7531\uff0c\u5f53\u7528\u6237\u8bbf\u95eeURL \/dashboard\u65f6\uff0c\u5c06\u4f7f\u7528auth\u4e2d\u95f4\u4ef6\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\uff0c\u4ee5\u786e\u4fdd\u7528\u6237\u5df2\u767b\u5f55\u3002\u5982\u679c\u7528\u6237\u672a\u901a\u8fc7\u9a8c\u8bc1\uff0cLaravel\u5c06\u81ea\u52a8\u91cd\u5b9a\u5411\u5230\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u767b\u5f55\u9875\u9762\u3002<\/p>\n<p>\u4e03\u3001\u7ed3\u8bba<\/p>\n<p>Laravel\u63d0\u4f9b\u4e86\u7b80\u5355\uff0c\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u8def\u7531\u7ed1\u5b9a\u6280\u672f\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u8def\u7531\u7ed1\u5b9a\u6280\u672f\uff0c\u5982\u5b9a\u4e49\u57fa\u672c\u8def\u7531\uff0c\u8def\u7531\u53c2\u6570\uff0c\u547d\u540d\u8def\u7531\uff0c\u8def\u7531\u5206\u7ec4\uff0c\u63a7\u5236\u5668\u548c\u4e2d\u95f4\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u6280\u672f\u6784\u5efa\u590d\u6742\u7684Web\u5e94\u7528\u7a0b\u5e8f\u548cAPI\u3002<\/p>\n<p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u8ba9\u60a8\u66f4\u597d\u5730\u4e86\u89e3Laravel\u4e2d\u8def\u7531\u7ed1\u5b9a\u7684\u5404\u79cd\u7528\u6cd5\u548c\u6280\u672f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel\u9879\u76ee\u4e2d\u5982\u4f55\u7ed1\u5b9a\u8def\u7531\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\u4f7f\u7528php\u8bed\u8a00\u7f16\u5199\u7684\u5f00\u6e90\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u5feb\u901f\u5f00\u53d1\u9ad8\u54c1\u8d28\u7684web\u5e94\u7528\u7a0b\u5e8f\u3002\u5728laravel\u6846\u67b6\u4e2d\uff0c\u8def\u7531\u662f\u5c06url\uff08uniform resource locator\uff09\u6620\u5c04\u5230\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7ed9\u5b9a\u7684\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002 \u5728Laravel\u4e2d\uff0c\u5c06URL\u548c\u5e94\u7528\u7a0b\u5e8f\u64cd\u4f5c\u6620\u5c04\u7684\u8fc7\u7a0b\u79f0\u4e3a\u8def\u7531\u7ed1\u5b9a\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5728Laravel\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5982\u4f55\u7ed1\u5b9a\u8def\u7531\u3002 \u4e00\u3001\u57fa\u672c\u8def\u7531\u7ed1\u5b9a \u5728Laravel\u4e2d\u5b9a\u4e49\u8def\u7531\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Route facade\u63d0\u4f9b\u7684\u65b9\u6cd5\u3002Route::get(), Route::post()\uff0cRoute::put()\u548cRoute::delete()\u7b49\u65b9\u6cd5\u8def\u7531\u7684HTTP\u8bf7\u6c42\u7c7b\u578b\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u8def\u7531\uff1a Route::get(&#8216;\/&#8217;, function () { return &#8216;Hello World&#8217;; }); \u767b\u5f55\u540e\u590d\u5236 \u4e0a\u9762\u7684\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u57fa\u672c\u8def\u7531\uff0c\u7528\u4e8e\u5904\u7406\u6839URL\uff08\/\uff09\u7684HTTP GET\u8bf7\u6c42\u3002\u5f53\u7528\u6237\u8bbf\u95ee\u6839URL\u65f6\uff0cLaravel\u5c06\u8fd4\u56de\u4e00\u4e2a\u201cHello World\u201d\u5b57\u7b26\u4e32\u3002 \u4e8c\u3001\u8def\u7531\u53c2\u6570\u7ed1\u5b9a \u73b0\u5728\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u5728Laravel\u4e2d\u7ed1\u5b9a\u5e26\u53c2\u6570\u7684\u8def\u7531\u3002 \u6211\u4eec\u4f7f\u7528{parameter}\u6765\u8868\u793a\u4e00\u4e2a\u8def\u7531\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5e26\u6709URL\u53c2\u6570\u7684\u8def\u7531\u3002 Route::get(&#8216;\/hello\/{name}&#8217;, function ($name) { return &#8216;Hello &#8216; . $name; }); \u767b\u5f55\u540e\u590d\u5236 \u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a\u201cname\u201d\u7684URL\u53c2\u6570\u3002\u5f53\u7528\u6237\u8bbf\u95ee\/hello\/{name} URL\u65f6\uff0cLaravel\u5c06\u628a\u53c2\u6570\u540d\u79f0\u6620\u5c04\u5230\u4f20\u9012\u7ed9\u5904\u7406\u7a0b\u5e8f\u7684\u53c2\u6570\u4e2d\u3002 \u4e09\u3001\u547d\u540d\u8def\u7531\u7ed1\u5b9a \u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u4e00\u4e2a\u7279\u5b9a\u7684\u8def\u7531\u5206\u914d\u4e00\u4e2a\u540d\u79f0\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u547d\u540d\u8def\u7531\u7ed1\u5b9a\u6765\u4e3a\u8def\u7531\u5206\u914d\u540d\u79f0\uff0c\u8fd9\u5728\u91cd\u6784\u4ee3\u7801\u65f6\u975e\u5e38\u6709\u7528\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u4e3a\u8def\u7531\u5206\u914d\u540d\u79f0\uff1a Route::get(&#8216;\/about&#8217;, function () { return &#8216;This is the about page&#8217;; })-&gt;name(&#8216;about&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528name()\u65b9\u6cd5\u5c06\u8def\u7531\u5206\u914d\u4e00\u4e2a\u540d\u79f0\uff0c\u540d\u79f0\u4e3a\u201cabout\u201d\u3002 \u56db\u3001\u8def\u7531\u5206\u7ec4 \u8def\u7531\u5206\u7ec4\u662f\u58f0\u660e\u5728\u540c\u4e00\u5206\u7ec4\u4e2d\u7684\u591a\u4e2a\u8def\u7531\u7684\u6280\u672f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5206\u7ec4\u5355\u72ec\u5b9a\u4e49\u8def\u7531\u5668\u7684\u5904\u7406\u7a0b\u5e8f\uff0c\u4e2d\u95f4\u4ef6\u6216\u524d\u7f00\u3002 [&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-22366","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22366","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=22366"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22366\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}