{"id":24520,"date":"2024-11-21T14:57:19","date_gmt":"2024-11-21T06:57:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24520\/"},"modified":"2024-11-21T14:57:19","modified_gmt":"2024-11-21T06:57:19","slug":"thinkphp6%e4%b8%ad%e9%97%b4%e4%bb%b6%e4%bd%bf%e7%94%a8%e6%8c%87%e5%8d%97%ef%bc%9a%e5%ae%9e%e7%8e%b0%e5%85%a8%e5%b1%80%e8%bf%87%e6%bb%a4%e4%b8%8e%e9%aa%8c%e8%af%81","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24520\/","title":{"rendered":"ThinkPHP6\u4e2d\u95f4\u4ef6\u4f7f\u7528\u6307\u5357\uff1a\u5b9e\u73b0\u5168\u5c40\u8fc7\u6ee4\u4e0e\u9a8c\u8bc1"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169302340684898.jpg\" class=\"aligncenter\" title=\"ThinkPHP6\u4e2d\u95f4\u4ef6\u4f7f\u7528\u6307\u5357\uff1a\u5b9e\u73b0\u5168\u5c40\u8fc7\u6ee4\u4e0e\u9a8c\u8bc1\u63d2\u56fe\" alt=\"ThinkPHP6\u4e2d\u95f4\u4ef6\u4f7f\u7528\u6307\u5357\uff1a\u5b9e\u73b0\u5168\u5c40\u8fc7\u6ee4\u4e0e\u9a8c\u8bc1\u63d2\u56fe\" \/><\/p>\n<p>ThinkPHP6\u4e2d\u95f4\u4ef6\u4f7f\u7528\u6307\u5357\uff1a\u5b9e\u73b0\u5168\u5c40\u8fc7\u6ee4\u4e0e\u9a8c\u8bc1<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u5728Web\u5e94\u7528\u5f00\u53d1\u4e2d\uff0c\u4e2d\u95f4\u4ef6\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u548c\u5e38\u7528\u7684\u6280\u672f\u624b\u6bb5\u3002ThinkPHP6\u4f5c\u4e3a\u4e00\u6b3e\u4e3b\u6d41\u7684PHP\u5f00\u53d1\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u4e2d\u95f4\u4ef6\u529f\u80fd\u3002\u4e2d\u95f4\u4ef6\u53ef\u4ee5\u5728\u8bf7\u6c42\u548c\u54cd\u5e94\u4e4b\u95f4\u8fdb\u884c\u5904\u7406\uff0c\u5b9e\u73b0\u5168\u5c40\u8fc7\u6ee4\u548c\u9a8c\u8bc1\u7684\u529f\u80fd\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<p>\u4e00\u3001\u4ec0\u4e48\u662f\u4e2d\u95f4\u4ef6\uff1f<br \/>\u4e2d\u95f4\u4ef6\uff08Middleware\uff09\u662f\u4ecb\u4e8e\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u4e00\u4e2a\u5904\u7406\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u73af\u8282\u3002\u5728ThinkPHP6\u4e2d\uff0c\u4e2d\u95f4\u4ef6\u4e3b\u8981\u7528\u4e8e\u62e6\u622a\u548c\u5904\u7406HTTP\u8bf7\u6c42\u3002\u4e2d\u95f4\u4ef6\u53ef\u4ee5\u9488\u5bf9\u4e0d\u540c\u7684\u8def\u7531\u6216\u5168\u5c40\u5e94\u7528\u5230\u6240\u6709\u7684\u8bf7\u6c42\u4e2d\u3002\u901a\u8fc7\u5bf9\u8bf7\u6c42\u6570\u636e\u7684\u8fc7\u6ee4\u3001\u9a8c\u8bc1\u548c\u9884\u5904\u7406\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u4e1a\u52a1\u903b\u8f91\u7684\u7cbe\u786e\u63a7\u5236\u3002<\/p>\n<p>\u4e8c\u3001ThinkPHP6\u4e2d\u95f4\u4ef6\u7684\u4f7f\u7528\u65b9\u6cd5<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\u521b\u5efa\u4e2d\u95f4\u4ef6\u6587\u4ef6<br \/>\u5728app\/middleware\u76ee\u5f55\u4e0b\u521b\u5efa\u4e2d\u95f4\u4ef6\u6587\u4ef6\uff0c\u4f8b\u5982CheckAuth.php\u3002\u4e2d\u95f4\u4ef6\u6587\u4ef6\u9700\u8981\u5305\u542b\u4e00\u4e2ahandle\u65b9\u6cd5\uff0c\u7528\u4e8e\u5904\u7406\u8bf7\u6c42\u548c\u54cd\u5e94\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4e2d\u95f4\u4ef6\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre>&lt;?php namespace appmiddleware;\n\nclass CheckAuth\n{\n    public function handle($request, Closure $next)\n    {\n        \/\/ \u5728\u5904\u7406\u8bf7\u6c42\u4e4b\u524d\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u8eab\u4efd\u9a8c\u8bc1\n        \n        \/\/ \u5c06\u8bf7\u6c42\u4f20\u9012\u7ed9\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u6216\u63a7\u5236\u5668\n        $response = $next($request);\n        \n        \/\/ \u5728\u54cd\u5e94\u4e4b\u540e\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u4fee\u6539\u54cd\u5e94\u6570\u636e\n        \n        return $response;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u6ce8\u518c\u4e2d\u95f4\u4ef6<br \/>\u5728app\/middleware.php\u6587\u4ef6\u4e2d\u6ce8\u518c\u4e2d\u95f4\u4ef6\u3002\u4f8b\u5982\uff0c\u5c06CheckAuth\u4e2d\u95f4\u4ef6\u6ce8\u518c\u4e3a\u5168\u5c40\u4e2d\u95f4\u4ef6\uff0c\u53ef\u4ee5\u5728\u6bcf\u4e2a\u8bf7\u6c42\u90fd\u81ea\u52a8\u5e94\u7528\u8be5\u4e2d\u95f4\u4ef6\uff1a<\/li>\n<\/ol>\n<pre>use appmiddlewareCheckAuth;\n\nreturn [\n    CheckAuth::class,\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u914d\u7f6e\u4e2d\u95f4\u4ef6\u89c4\u5219<br \/>\u5728app\/route\/middleware.php\u6587\u4ef6\u4e2d\u914d\u7f6e\u4e2d\u95f4\u4ef6\u89c4\u5219\uff0c\u4ee5\u4fbf\u6307\u5b9a\u5728\u54ea\u4e9b\u8def\u7531\u4e2d\u5e94\u7528\u4e2d\u95f4\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre>use appmiddlewareCheckAuth;\n\nreturn [\n    'auth' =&amp;gt; [\n        'appindexcontrollerUser@index',\n    ],\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u4e2d\u95f4\u4ef6\u7684\u8def\u7531\u9650\u5b9a<br \/>\u5728\u8def\u7531\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u65f6\uff0c\u53ef\u4ee5\u5728\u8def\u7531\u5b9a\u4e49\u4e2d\u4f7f\u7528\u4e00\u5bf9\u4e2d\u62ec\u53f7\u6765\u9650\u5b9a\u4e2d\u95f4\u4ef6\u7684\u5e94\u7528\u8303\u56f4\uff1a<\/li>\n<\/ol>\n<pre>Route::get('user\/profile', '[auth]appindexcontrollerUser@profile');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u8def\u7531\u4e2d\uff0c\u4e2d\u95f4\u4ef6auth\u53ea\u4f1a\u5e94\u7528\u4e8euser\/profile\u8fd9\u4e2a\u5177\u4f53\u7684\u8def\u7531\u3002<\/p>\n<p>\u4e09\u3001\u4e2d\u95f4\u4ef6\u5b9e\u4f8b\uff1a\u8bf7\u6c42\u53c2\u6570\u9a8c\u8bc1<br \/>\u4e2d\u95f4\u4ef6\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5bf9\u8bf7\u6c42\u53c2\u6570\u8fdb\u884c\u9a8c\u8bc1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u8bf7\u6c42\u53c2\u6570\u9a8c\u8bc1\u4e2d\u95f4\u4ef6\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php namespace appmiddleware;\n\nclass CheckParams\n{\n    public function handle($request, Closure $next)\n    {\n        \/\/ \u83b7\u53d6\u8bf7\u6c42\u53c2\u6570\n        $params = $request-&gt;param();\n        \n        \/\/ \u9a8c\u8bc1\u8bf7\u6c42\u53c2\u6570\n        $validate = new     hinkValidate;\n        $validate-&amp;gt;rule([\n            'name'  =&amp;gt; 'require',\n            'age' =&amp;gt; 'integer|between:1,100',\n        ]);\n        if (!$validate-&amp;gt;check($params)) {\n            return json($validate-&amp;gt;getError(), 400);\n        }\n        \n        return $next($request);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4e2d\u95f4\u4ef6\u4f1a\u9a8c\u8bc1\u8bf7\u6c42\u4e2d\u7684name\u548cage\u53c2\u6570\u662f\u5426\u7b26\u5408\u89c4\u5219\uff0c\u5982\u679c\u4e0d\u7b26\u5408\u5219\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p>\u56db\u3001\u603b\u7ed3<br \/>\u4e2d\u95f4\u4ef6\u662fThinkPHP6\u6846\u67b6\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\u4e4b\u4e00\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u5168\u5c40\u8fc7\u6ee4\u548c\u9a8c\u8bc1\u7684\u9700\u6c42\u3002\u901a\u8fc7\u521b\u5efa\u4e2d\u95f4\u4ef6\u6587\u4ef6\u3001\u6ce8\u518c\u4e2d\u95f4\u4ef6\u3001\u914d\u7f6e\u4e2d\u95f4\u4ef6\u89c4\u5219\u4ee5\u53ca\u5728\u8def\u7531\u4e2d\u9650\u5b9a\u4e2d\u95f4\u4ef6\u7684\u5e94\u7528\u8303\u56f4\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u5bf9\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u7cbe\u786e\u63a7\u5236\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4e86\u89e3\u4e86\u4e2d\u95f4\u4ef6\u5982\u4f55\u5e94\u7528\u4e8e\u8bf7\u6c42\u53c2\u6570\u7684\u9a8c\u8bc1\uff0c\u5e2e\u52a9\u6211\u4eec\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u548c\u7a33\u5b9a\u6027\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u60a8\u7406\u89e3\u548c\u4f7f\u7528ThinkPHP6\u4e2d\u95f4\u4ef6\u63d0\u4f9b\u4e00\u4e9b\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fThinkPHP6\u4e2d\u95f4\u4ef6\u4f7f\u7528\u6307\u5357\uff1a\u5b9e\u73b0\u5168\u5c40\u8fc7\u6ee4\u4e0e\u9a8c\u8bc1\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>ThinkPHP6\u4e2d\u95f4\u4ef6\u4f7f\u7528\u6307\u5357\uff1a\u5b9e\u73b0\u5168\u5c40\u8fc7\u6ee4\u4e0e\u9a8c\u8bc1 \u5f15\u8a00\uff1a\u5728Web\u5e94\u7528\u5f00\u53d1\u4e2d\uff0c\u4e2d\u95f4\u4ef6\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u548c\u5e38\u7528\u7684\u6280\u672f\u624b\u6bb5\u3002ThinkPHP6\u4f5c\u4e3a\u4e00\u6b3e\u4e3b\u6d41\u7684PHP\u5f00\u53d1\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u4e2d\u95f4\u4ef6\u529f\u80fd\u3002\u4e2d\u95f4\u4ef6\u53ef\u4ee5\u5728\u8bf7\u6c42\u548c\u54cd\u5e94\u4e4b\u95f4\u8fdb\u884c\u5904\u7406\uff0c\u5b9e\u73b0\u5168\u5c40\u8fc7\u6ee4\u548c\u9a8c\u8bc1\u7684\u529f\u80fd\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u548c\u7a33\u5b9a\u6027\u3002 \u4e00\u3001\u4ec0\u4e48\u662f\u4e2d\u95f4\u4ef6\uff1f\u4e2d\u95f4\u4ef6\uff08Middleware\uff09\u662f\u4ecb\u4e8e\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u4e00\u4e2a\u5904\u7406\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u73af\u8282\u3002\u5728ThinkPHP6\u4e2d\uff0c\u4e2d\u95f4\u4ef6\u4e3b\u8981\u7528\u4e8e\u62e6\u622a\u548c\u5904\u7406HTTP\u8bf7\u6c42\u3002\u4e2d\u95f4\u4ef6\u53ef\u4ee5\u9488\u5bf9\u4e0d\u540c\u7684\u8def\u7531\u6216\u5168\u5c40\u5e94\u7528\u5230\u6240\u6709\u7684\u8bf7\u6c42\u4e2d\u3002\u901a\u8fc7\u5bf9\u8bf7\u6c42\u6570\u636e\u7684\u8fc7\u6ee4\u3001\u9a8c\u8bc1\u548c\u9884\u5904\u7406\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u4e1a\u52a1\u903b\u8f91\u7684\u7cbe\u786e\u63a7\u5236\u3002 \u4e8c\u3001ThinkPHP6\u4e2d\u95f4\u4ef6\u7684\u4f7f\u7528\u65b9\u6cd5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u521b\u5efa\u4e2d\u95f4\u4ef6\u6587\u4ef6\u5728app\/middleware\u76ee\u5f55\u4e0b\u521b\u5efa\u4e2d\u95f4\u4ef6\u6587\u4ef6\uff0c\u4f8b\u5982CheckAuth.php\u3002\u4e2d\u95f4\u4ef6\u6587\u4ef6\u9700\u8981\u5305\u542b\u4e00\u4e2ahandle\u65b9\u6cd5\uff0c\u7528\u4e8e\u5904\u7406\u8bf7\u6c42\u548c\u54cd\u5e94\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4e2d\u95f4\u4ef6\u793a\u4f8b\uff1a &lt;?php namespace appmiddleware; class CheckAuth { public function handle($request, Closure $next) { \/\/ \u5728\u5904\u7406\u8bf7\u6c42\u4e4b\u524d\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u8eab\u4efd\u9a8c\u8bc1 \/\/ \u5c06\u8bf7\u6c42\u4f20\u9012\u7ed9\u4e0b\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u6216\u63a7\u5236\u5668 $response = $next($request); \/\/ \u5728\u54cd\u5e94\u4e4b\u540e\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u4fee\u6539\u54cd\u5e94\u6570\u636e return $response; } } \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u518c\u4e2d\u95f4\u4ef6\u5728app\/middleware.php\u6587\u4ef6\u4e2d\u6ce8\u518c\u4e2d\u95f4\u4ef6\u3002\u4f8b\u5982\uff0c\u5c06CheckAuth\u4e2d\u95f4\u4ef6\u6ce8\u518c\u4e3a\u5168\u5c40\u4e2d\u95f4\u4ef6\uff0c\u53ef\u4ee5\u5728\u6bcf\u4e2a\u8bf7\u6c42\u90fd\u81ea\u52a8\u5e94\u7528\u8be5\u4e2d\u95f4\u4ef6\uff1a use appmiddlewareCheckAuth; return [ CheckAuth::class, ]; \u767b\u5f55\u540e\u590d\u5236 \u914d\u7f6e\u4e2d\u95f4\u4ef6\u89c4\u5219\u5728app\/route\/middleware.php\u6587\u4ef6\u4e2d\u914d\u7f6e\u4e2d\u95f4\u4ef6\u89c4\u5219\uff0c\u4ee5\u4fbf\u6307\u5b9a\u5728\u54ea\u4e9b\u8def\u7531\u4e2d\u5e94\u7528\u4e2d\u95f4\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a use appmiddlewareCheckAuth; return [ &#8216;auth&#8217; =&amp;gt; [ &#8216;appindexcontrollerUser@index&#8217;, ], ]; \u767b\u5f55\u540e\u590d\u5236 \u4e2d\u95f4\u4ef6\u7684\u8def\u7531\u9650\u5b9a\u5728\u8def\u7531\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u65f6\uff0c\u53ef\u4ee5\u5728\u8def\u7531\u5b9a\u4e49\u4e2d\u4f7f\u7528\u4e00\u5bf9\u4e2d\u62ec\u53f7\u6765\u9650\u5b9a\u4e2d\u95f4\u4ef6\u7684\u5e94\u7528\u8303\u56f4\uff1a Route::get(&#8216;user\/profile&#8217;, &#8216;[auth]appindexcontrollerUser@profile&#8217;); [&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-24520","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24520","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=24520"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24520\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}