{"id":25295,"date":"2024-11-21T17:52:50","date_gmt":"2024-11-21T09:52:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25295\/"},"modified":"2024-11-21T17:52:50","modified_gmt":"2024-11-21T09:52:50","slug":"thinkphp%e6%9d%83%e9%99%90%e7%ae%a1%e7%90%86%e6%80%8e%e4%b9%88%e5%81%9a","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25295\/","title":{"rendered":"thinkphp\u6743\u9650\u7ba1\u7406\u600e\u4e48\u505a"},"content":{"rendered":"<blockquote><p>\n  thinkphp \u6743\u9650\u7ba1\u7406\u901a\u8fc7\u5b9a\u4e49\u6743\u9650\u89c4\u5219\u5e76\u521b\u5efa\u6743\u9650\u3001\u7528\u6237\u89d2\u8272\u548c\u89d2\u8272\u6743\u9650\u6a21\u578b\u5b9e\u73b0\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5b9a\u4e49\u6743\u9650\u89c4\u5219\u3001\u521b\u5efa\u6743\u9650\u6a21\u578b\u3001\u521b\u5efa\u7528\u6237\u89d2\u8272\u6a21\u578b\u3001\u521b\u5efa\u89d2\u8272\u6743\u9650\u6a21\u578b\u3001\u914d\u7f6e\u6743\u9650\u9a8c\u8bc1\u89c4\u5219\u3001\u7f16\u5199\u6838\u5fc3\u903b\u8f91\u548c\u96c6\u6210\u5230\u5e94\u7528\u4e2d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202404\/09\/2024040917302639201.jpg\" class=\"aligncenter\" title=\"thinkphp\u6743\u9650\u7ba1\u7406\u600e\u4e48\u505a\u63d2\u56fe\" alt=\"thinkphp\u6743\u9650\u7ba1\u7406\u600e\u4e48\u505a\u63d2\u56fe\" \/><\/p>\n<p><strong>ThinkPHP\u6743\u9650\u7ba1\u7406<\/strong><\/p>\n<p><strong>\u5982\u4f55\u5b9e\u73b0 ThinkPHP \u6743\u9650\u7ba1\u7406\uff1f<\/strong><\/p>\n<p>ThinkPHP \u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u6743\u9650\u7ba1\u7406\u673a\u5236\uff0c\u53ef\u7528\u4e8e\u5b9a\u4e49\u548c\u7ba1\u7406\u7528\u6237\u7684\u6743\u9650\u3002\u4ee5\u4e0b\u6b65\u9aa4\u8bf4\u660e\u4e86\u5982\u4f55\u5b9e\u73b0 ThinkPHP \u6743\u9650\u7ba1\u7406\uff1a<\/p>\n<p><strong>1. \u5b9a\u4e49\u6743\u9650\u89c4\u5219<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5b9a\u4e49\u6743\u9650\u89c4\u5219\u3002\u6743\u9650\u89c4\u5219\u53ef\u4ee5\u662f\u57fa\u4e8e\u64cd\u4f5c\u3001\u6a21\u5757\u6216\u5176\u4ed6\u81ea\u5b9a\u4e49\u6807\u51c6\u7684\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>namespace appmodel;\n\nuse thinkModel;\n\nclass PermissionRule extends Model\n{\n    \/\/ \u64cd\u4f5c\u6743\u9650\n    const OPERATION_PERMISSION = 1;\n    \/\/ \u6a21\u5757\u6743\u9650\n    const MODULE_PERMISSION = 2;\n    \/\/ ...\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u521b\u5efa\u6743\u9650\u6a21\u578b<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u6a21\u578b\u7c7b\u6765\u7ba1\u7406\u6743\u9650\u3002\u8be5\u6a21\u578b\u5e94\u7ee7\u627f ThinkPHP \u7684 Model \u7c7b\uff0c\u5e76\u5b9a\u4e49\u5fc5\u8981\u7684\u5b57\u6bb5\uff08\u4f8b\u5982\u6743\u9650\u540d\u79f0\u3001\u7c7b\u578b\u7b49\uff09\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>namespace appmodel;\n\nuse thinkModel;\n\nclass Permission extends Model\n{\n    protected $table = 'sys_permission';\n    protected $fields = ['permission_id', 'permission_name', 'permission_type', ...];\n    \/\/ ...\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u521b\u5efa\u7528\u6237\u89d2\u8272\u6a21\u578b<\/strong><\/p>\n<p>\u521b\u5efa\u53e6\u4e00\u4e2a\u6a21\u578b\u7c7b\u6765\u7ba1\u7406\u7528\u6237\u89d2\u8272\u3002\u8be5\u6a21\u578b\u5e94\u5305\u542b\u7528\u6237\u4e0e\u89d2\u8272\u4e4b\u95f4\u7684\u5173\u8054\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>namespace appmodel;\n\nuse thinkModel;\n\nclass UserRole extends Model\n{\n    protected $table = 'sys_user_role';\n    protected $fields = ['user_id', 'role_id'];\n    \/\/ ...\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u521b\u5efa\u89d2\u8272\u6743\u9650\u6a21\u578b<\/strong><\/p>\n<p>\u521b\u5efa\u7b2c\u4e09\u4e2a\u6a21\u578b\u7c7b\u6765\u7ba1\u7406\u89d2\u8272\u4e0e\u6743\u9650\u4e4b\u95f4\u7684\u5173\u8054\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>namespace appmodel;\n\nuse thinkModel;\n\nclass RolePermission extends Model\n{\n    protected $table = 'sys_role_permission';\n    protected $fields = ['role_id', 'permission_id'];\n    \/\/ ...\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u914d\u7f6e\u89c4\u5219<\/strong><\/p>\n<p>\u5728 app\/extra \u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a permission.php \u6587\u4ef6\uff0c\u5e76\u914d\u7f6e\u6743\u9650\u9a8c\u8bc1\u89c4\u5219\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>return [\n    'check_mode' =&gt; 'logic', \/\/ \u6743\u9650\u68c0\u67e5\u6a21\u5f0f\uff1alogic OR url\n    'auth_rule' =&gt; [\n        \/\/ ...\n    ],\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>6. \u7f16\u5199\u6838\u5fc3\u903b\u8f91<\/strong><\/p>\n<p>\u5728 app\/common\/middleware\/CheckPermission.php \u6587\u4ef6\u4e2d\u7f16\u5199\u6838\u5fc3\u6743\u9650\u68c0\u67e5\u903b\u8f91\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>class CheckPermission\n{\n    public function handle($request, Closure $next)\n    {\n        \/\/ ...\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>7. \u96c6\u6210\u5230\u5e94\u7528\u4e2d<\/strong><\/p>\n<p>\u5728 app\/route.php \u6587\u4ef6\u4e2d\uff0c\u5c06 CheckPermission \u4e2d\u95f4\u4ef6\u6ce8\u518c\u5230\u8def\u7531\u89c4\u5219\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>\/\/ \u542f\u7528\u8def\u7531\u9274\u6743\n$router-&gt;middleware([\n    ppcommonmiddlewareCheckPermission::class,\n]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u60a8\u5c31\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u5168\u9762\u4e14\u7075\u6d3b\u7684 ThinkPHP \u6743\u9650\u7ba1\u7406\u7cfb\u7edf\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6743\u9650\u7ba1\u7406\u600e\u4e48\u505a\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>thinkphp \u6743\u9650\u7ba1\u7406\u901a\u8fc7\u5b9a\u4e49\u6743\u9650\u89c4\u5219\u5e76\u521b\u5efa\u6743\u9650\u3001\u7528\u6237\u89d2\u8272\u548c\u89d2\u8272\u6743\u9650\u6a21\u578b\u5b9e\u73b0\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5b9a\u4e49\u6743\u9650\u89c4\u5219\u3001\u521b\u5efa\u6743\u9650\u6a21\u578b\u3001\u521b\u5efa\u7528\u6237\u89d2\u8272\u6a21\u578b\u3001\u521b\u5efa\u89d2\u8272\u6743\u9650\u6a21\u578b\u3001\u914d\u7f6e\u6743\u9650\u9a8c\u8bc1\u89c4\u5219\u3001\u7f16\u5199\u6838\u5fc3\u903b\u8f91\u548c\u96c6\u6210\u5230\u5e94\u7528\u4e2d\u3002 ThinkPHP\u6743\u9650\u7ba1\u7406 \u5982\u4f55\u5b9e\u73b0 ThinkPHP \u6743\u9650\u7ba1\u7406\uff1f ThinkPHP \u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u6743\u9650\u7ba1\u7406\u673a\u5236\uff0c\u53ef\u7528\u4e8e\u5b9a\u4e49\u548c\u7ba1\u7406\u7528\u6237\u7684\u6743\u9650\u3002\u4ee5\u4e0b\u6b65\u9aa4\u8bf4\u660e\u4e86\u5982\u4f55\u5b9e\u73b0 ThinkPHP \u6743\u9650\u7ba1\u7406\uff1a 1. \u5b9a\u4e49\u6743\u9650\u89c4\u5219 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u9996\u5148\uff0c\u9700\u8981\u5b9a\u4e49\u6743\u9650\u89c4\u5219\u3002\u6743\u9650\u89c4\u5219\u53ef\u4ee5\u662f\u57fa\u4e8e\u64cd\u4f5c\u3001\u6a21\u5757\u6216\u5176\u4ed6\u81ea\u5b9a\u4e49\u6807\u51c6\u7684\u3002\u4f8b\u5982\uff1a namespace appmodel; use thinkModel; class PermissionRule extends Model { \/\/ \u64cd\u4f5c\u6743\u9650 const OPERATION_PERMISSION = 1; \/\/ \u6a21\u5757\u6743\u9650 const MODULE_PERMISSION = 2; \/\/ &#8230; } \u767b\u5f55\u540e\u590d\u5236 2. \u521b\u5efa\u6743\u9650\u6a21\u578b \u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u6a21\u578b\u7c7b\u6765\u7ba1\u7406\u6743\u9650\u3002\u8be5\u6a21\u578b\u5e94\u7ee7\u627f ThinkPHP \u7684 Model \u7c7b\uff0c\u5e76\u5b9a\u4e49\u5fc5\u8981\u7684\u5b57\u6bb5\uff08\u4f8b\u5982\u6743\u9650\u540d\u79f0\u3001\u7c7b\u578b\u7b49\uff09\u3002\u4f8b\u5982\uff1a namespace appmodel; use thinkModel; class Permission extends Model { [&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-25295","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25295","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=25295"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25295\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}