{"id":24831,"date":"2024-11-21T13:37:54","date_gmt":"2024-11-21T05:37:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24831\/"},"modified":"2024-11-21T13:37:54","modified_gmt":"2024-11-21T05:37:54","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8laravel%e5%ae%9e%e7%8e%b0%e7%94%a8%e6%88%b7%e6%9d%83%e9%99%90%e7%ae%a1%e7%90%86%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24831\/","title":{"rendered":"\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u7528\u6237\u6743\u9650\u7ba1\u7406\u529f\u80fd"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169890535534754.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u7528\u6237\u6743\u9650\u7ba1\u7406\u529f\u80fd\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u7528\u6237\u6743\u9650\u7ba1\u7406\u529f\u80fd\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u529f\u80fd<\/p>\n<p>\u968f\u7740Web\u5e94\u7528\u7a0b\u5e8f\u7684\u53d1\u5c55\uff0c\u7528\u6237\u6743\u9650\u7ba1\u7406\u5728\u8bb8\u591a\u9879\u76ee\u4e2d\u53d8\u5f97\u8d8a\u6765\u8d8a\u91cd\u8981\u3002Laravel\u4f5c\u4e3a\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u4e3a\u5904\u7406\u7528\u6237\u6743\u9650\u7ba1\u7406\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u5de5\u5177\u548c\u529f\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Laravel\u5b9e\u73b0\u7528\u6237\u6743\u9650\u7ba1\u7406\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<ol>\n<li>\u6570\u636e\u5e93\u8bbe\u8ba1<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u8ba1\u4e00\u4e2a\u6570\u636e\u5e93\u6a21\u578b\u6765\u5b58\u50a8\u7528\u6237\u3001\u89d2\u8272\u548c\u6743\u9650\u7684\u5173\u7cfb\u3002\u4e3a\u4e86\u7b80\u5316\u64cd\u4f5c\uff0c\u6211\u4eec\u5c06\u4f7f\u7528Laravel\u81ea\u5e26\u7684\u8fc1\u79fb\u5de5\u5177\u6765\u521b\u5efa\u6570\u636e\u5e93\u8868\u3002\u6253\u5f00\u547d\u4ee4\u884c\u5de5\u5177\u5e76\u5207\u6362\u5230\u9879\u76ee\u6839\u76ee\u5f55\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u8fc1\u79fb\u6587\u4ef6\uff1a<\/p>\n<pre>php artisan make:migration create_roles_table --create=roles\nphp artisan make:migration create_permissions_table --create=permissions\nphp artisan make:migration create_role_user_table --create=role_user\nphp artisan make:migration create_permission_role_table --create=permission_role<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\u5728database\/migrations\u76ee\u5f55\u4e2d\u627e\u5230\u751f\u6210\u7684\u8fc1\u79fb\u6587\u4ef6\uff0c\u5e76\u7f16\u8f91\u5b83\u4eec\u3002\u4ee5\u4e0b\u4e3a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>\/\/ roles\u8868\u8fc1\u79fb\u6587\u4ef6\npublic function up()\n{\n    Schema::create('roles', function (Blueprint $table) {\n        $table-&amp;gt;increments('id');\n        $table-&amp;gt;string('name')-&amp;gt;unique();\n        $table-&amp;gt;timestamps();\n    });\n}\n\n\/\/ permissions\u8868\u8fc1\u79fb\u6587\u4ef6\npublic function up()\n{\n    Schema::create('permissions', function (Blueprint $table) {\n        $table-&amp;gt;increments('id');\n        $table-&amp;gt;string('name')-&amp;gt;unique();\n        $table-&amp;gt;timestamps();\n    });\n}\n\n\/\/ role_user\u5173\u8054\u8868\u8fc1\u79fb\u6587\u4ef6\npublic function up()\n{\n    Schema::create('role_user', function (Blueprint $table) {\n        $table-&amp;gt;integer('role_id')-&amp;gt;unsigned();\n        $table-&amp;gt;integer('user_id')-&amp;gt;unsigned();\n        $table-&amp;gt;foreign('role_id')-&amp;gt;references('id')-&amp;gt;on('roles');\n        $table-&amp;gt;foreign('user_id')-&amp;gt;references('id')-&amp;gt;on('users');\n    });\n}\n\n\/\/ permission_role\u5173\u8054\u8868\u8fc1\u79fb\u6587\u4ef6\npublic function up()\n{\n    Schema::create('permission_role', function (Blueprint $table) {\n        $table-&amp;gt;integer('permission_id')-&amp;gt;unsigned();\n        $table-&amp;gt;integer('role_id')-&amp;gt;unsigned();\n        $table-&amp;gt;foreign('permission_id')-&amp;gt;references('id')-&amp;gt;on('permissions');\n        $table-&amp;gt;foreign('role_id')-&amp;gt;references('id')-&amp;gt;on('roles');\n    });\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5b8c\u6210\u8fc1\u79fb\u6587\u4ef6\u7684\u7f16\u8f91\u540e\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u6267\u884c\u8fc1\u79fb\uff1a<\/p>\n<pre>php artisan migrate<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u521b\u5efa\u6a21\u578b\u548c\u5173\u7cfb<\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efaLaravel\u6a21\u578b\u6765\u6620\u5c04\u6570\u636e\u5e93\u8868\u5e76\u5efa\u7acb\u5b83\u4eec\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u6253\u5f00\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u6a21\u578b\u6587\u4ef6\uff1a<\/p>\n<pre>php artisan make:model Role\nphp artisan make:model Permission<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\u6253\u5f00\u751f\u6210\u7684\u6a21\u578b\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>\/\/ Role\u6a21\u578b\nclass Role extends Model\n{\n    public function users()\n    {\n        return $this-&amp;gt;belongsToMany(User::class);\n    }\n\n    public function permissions()\n    {\n        return $this-&amp;gt;belongsToMany(Permission::class);\n    }\n}\n\n\/\/ Permission\u6a21\u578b\nclass Permission extends Model\n{\n    public function roles()\n    {\n        return $this-&amp;gt;belongsToMany(Role::class);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u6dfb\u52a0\u7528\u6237\u5173\u8054<\/li>\n<\/ol>\n<p>\u6253\u5f00User\u6a21\u578b\u6587\u4ef6\uff0c\u5728\u7c7b\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<pre>public function roles()\n{\n    return $this-&amp;gt;belongsToMany(Role::class);\n}\n\npublic function hasRole($role)\n{\n    if (is_string($role)) {\n        return $this-&amp;gt;roles-&amp;gt;contains('name', $role);\n    }\n\n    return !! $role-&amp;gt;intersect($this-&amp;gt;roles)-&amp;gt;count();\n}\n\npublic function assignRole($role)\n{\n    return $this-&amp;gt;roles()-&amp;gt;save(\n        Role::whereName($role)-&amp;gt;firstOrFail()\n    );\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee3\u7801\u4e2d\uff0cRole\u6a21\u578b\u4f7f\u7528\u4e86belongsToMany\u65b9\u6cd5\u5efa\u7acb\u4e86\u4e0eUser\u6a21\u578b\u4e4b\u95f4\u7684\u591a\u5bf9\u591a\u5173\u7cfb\uff0chasRole\u65b9\u6cd5\u7528\u4e8e\u5224\u65ad\u7528\u6237\u662f\u5426\u62e5\u6709\u67d0\u4e2a\u89d2\u8272\uff0cassignRole\u65b9\u6cd5\u7528\u4e8e\u7ed9\u7528\u6237\u5206\u914d\u89d2\u8272\u3002<\/p>\n<ol>\n<li>\u6dfb\u52a0\u6743\u9650\u5173\u8054<\/li>\n<\/ol>\n<p>\u5728Role\u6a21\u578b\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5b9a\u4e49\u4e86\u4e0ePermission\u6a21\u578b\u4e4b\u95f4\u7684\u591a\u5bf9\u591a\u5173\u7cfb\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5df2\u6709\u7684\u65b9\u6cd5\u3002<\/p>\n<ol>\n<li>\u4e2d\u95f4\u4ef6\u914d\u7f6e<\/li>\n<\/ol>\n<p>Laravel\u63d0\u4f9b\u4e86\u4e2d\u95f4\u4ef6\u529f\u80fd\u6765\u5b9e\u73b0\u5bf9\u8def\u7531\u7684\u6743\u9650\u63a7\u5236\uff0c\u6211\u4eec\u9700\u8981\u914d\u7f6e\u4e2d\u95f4\u4ef6\u6765\u9650\u5236\u7528\u6237\u7684\u8bbf\u95ee\u3002\u6253\u5f00app\/Http\/Kernel.php\u6587\u4ef6\uff0c\u5728$routeMiddleware\u6570\u7ec4\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>'role' =&amp;gt; AppHttpMiddlewareRoleMiddleware::class,\n'permission' =&amp;gt; AppHttpMiddlewarePermissionMiddleware::class,<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u521b\u5efa\u4e2d\u95f4\u4ef6<\/li>\n<\/ol>\n<p>\u5728\u547d\u4ee4\u884c\u5de5\u5177\u4e2d\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u4e2d\u95f4\u4ef6\u6587\u4ef6\uff1a<\/p>\n<pre>php artisan make:middleware RoleMiddleware\nphp artisan make:middleware PermissionMiddleware<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\u6253\u5f00\u751f\u6210\u7684\u4e2d\u95f4\u4ef6\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>\/\/ RoleMiddleware\nclass RoleMiddleware\n{\n    public function handle($request, Closure $next, $role)\n    {\n        if (! $request-&amp;gt;user()-&amp;gt;hasRole($role)) {\n            abort(403, 'Unauthorized');\n        }\n\n        return $next($request);\n    }\n}\n\n\/\/ PermissionMiddleware\nclass PermissionMiddleware\n{\n    public function handle($request, Closure $next, $permission)\n    {\n        if (! $request-&amp;gt;user()-&amp;gt;hasPermissionTo($permission)) {\n            abort(403, 'Unauthorized');\n        }\n\n        return $next($request);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee3\u7801\u4e2d\uff0cRoleMiddleware\u68c0\u67e5\u7528\u6237\u662f\u5426\u62e5\u6709\u6307\u5b9a\u89d2\u8272\uff0cPermissionMiddleware\u68c0\u67e5\u7528\u6237\u662f\u5426\u5177\u6709\u6307\u5b9a\u6743\u9650\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528\u4e2d\u95f4\u4ef6<\/li>\n<\/ol>\n<p>\u73b0\u5728\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u8fdb\u884c\u6743\u9650\u63a7\u5236\u7684\u8def\u7531\u4e0a\u4f7f\u7528\u6211\u4eec\u5b9a\u4e49\u7684\u4e2d\u95f4\u4ef6\u6765\u9650\u5236\u8bbf\u95ee\u3002\u5728\u8def\u7531\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528middleware\u65b9\u6cd5\u5e76\u4f20\u5165\u4e2d\u95f4\u4ef6\u540d\u79f0\uff0c\u5982\u540c\u4ee5\u4e0b\u793a\u4f8b\uff1a<\/p>\n<pre>Route::get('\/admin', function () {\n    \/\/ \u9650\u5236\u53ea\u6709\u62e5\u6709admin\u89d2\u8272\u7684\u7528\u6237\u624d\u80fd\u8bbf\u95ee\n})-&amp;gt;middleware('role:admin');\n\nRoute::get('\/delete-user', function () {\n    \/\/ \u9650\u5236\u53ea\u6709\u62e5\u6709delete-user\u6743\u9650\u7684\u7528\u6237\u624d\u80fd\u8bbf\u95ee\n})-&amp;gt;middleware('permission:delete-user');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u81f3\u6b64\uff0c\u6211\u4eec\u5df2\u7ecf\u5b9e\u73b0\u4e86\u4f7f\u7528Laravel\u8fdb\u884c\u7528\u6237\u6743\u9650\u7ba1\u7406\u7684\u529f\u80fd\uff0c\u5e76\u901a\u8fc7\u4e2d\u95f4\u4ef6\u9650\u5236\u4e86\u8bbf\u95ee\u6743\u9650\u3002\u901a\u8fc7\u6570\u636e\u5e93\u6a21\u578b\u3001\u5173\u8054\u5173\u7cfb\u3001\u4e2d\u95f4\u4ef6\u7b49\u529f\u80fd\u7684\u4f7f\u7528\uff0c\u5b9e\u73b0\u4e86\u7528\u6237\u3001\u89d2\u8272\u548c\u6743\u9650\u4e4b\u95f4\u7684\u7075\u6d3b\u7ba1\u7406\u4e0e\u63a7\u5236\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<\/p>\n<p>\u7528\u6237\u6743\u9650\u7ba1\u7406\u662fWeb\u5e94\u7528\u7a0b\u5e8f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0cLaravel\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u548c\u529f\u80fd\u6765\u5b9e\u73b0\u8fd9\u4e00\u9700\u6c42\u3002\u672c\u6587\u901a\u8fc7\u6570\u636e\u5e93\u8bbe\u8ba1\u3001\u6a21\u578b\u5173\u8054\u3001\u4e2d\u95f4\u4ef6\u914d\u7f6e\u7b49\u8be6\u7ec6\u6b65\u9aa4\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u7528\u6237\u6743\u9650\u7ba1\u7406\u529f\u80fd\uff0c\u5e76\u7ed9\u51fa\u4e86\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728\u8fdb\u884c\u7528\u6237\u6743\u9650\u7ba1\u7406\u65f6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u529f\u80fd\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>\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u529f\u80fd \u968f\u7740Web\u5e94\u7528\u7a0b\u5e8f\u7684\u53d1\u5c55\uff0c\u7528\u6237\u6743\u9650\u7ba1\u7406\u5728\u8bb8\u591a\u9879\u76ee\u4e2d\u53d8\u5f97\u8d8a\u6765\u8d8a\u91cd\u8981\u3002Laravel\u4f5c\u4e3a\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u4e3a\u5904\u7406\u7528\u6237\u6743\u9650\u7ba1\u7406\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u5de5\u5177\u548c\u529f\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Laravel\u5b9e\u73b0\u7528\u6237\u6743\u9650\u7ba1\u7406\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u6570\u636e\u5e93\u8bbe\u8ba1 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u8ba1\u4e00\u4e2a\u6570\u636e\u5e93\u6a21\u578b\u6765\u5b58\u50a8\u7528\u6237\u3001\u89d2\u8272\u548c\u6743\u9650\u7684\u5173\u7cfb\u3002\u4e3a\u4e86\u7b80\u5316\u64cd\u4f5c\uff0c\u6211\u4eec\u5c06\u4f7f\u7528Laravel\u81ea\u5e26\u7684\u8fc1\u79fb\u5de5\u5177\u6765\u521b\u5efa\u6570\u636e\u5e93\u8868\u3002\u6253\u5f00\u547d\u4ee4\u884c\u5de5\u5177\u5e76\u5207\u6362\u5230\u9879\u76ee\u6839\u76ee\u5f55\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u8fc1\u79fb\u6587\u4ef6\uff1a php artisan make:migration create_roles_table &#8211;create=roles php artisan make:migration create_permissions_table &#8211;create=permissions php artisan make:migration create_role_user_table &#8211;create=role_user php artisan make:migration create_permission_role_table &#8211;create=permission_role \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\u5728database\/migrations\u76ee\u5f55\u4e2d\u627e\u5230\u751f\u6210\u7684\u8fc1\u79fb\u6587\u4ef6\uff0c\u5e76\u7f16\u8f91\u5b83\u4eec\u3002\u4ee5\u4e0b\u4e3a\u793a\u4f8b\u4ee3\u7801\uff1a \/\/ roles\u8868\u8fc1\u79fb\u6587\u4ef6 public function up() { Schema::create(&#8216;roles&#8217;, function (Blueprint $table) { $table-&amp;gt;increments(&#8216;id&#8217;); $table-&amp;gt;string(&#8216;name&#8217;)-&amp;gt;unique(); $table-&amp;gt;timestamps(); }); } \/\/ permissions\u8868\u8fc1\u79fb\u6587\u4ef6 public function up() { Schema::create(&#8216;permissions&#8217;, function (Blueprint $table) { $table-&amp;gt;increments(&#8216;id&#8217;); $table-&amp;gt;string(&#8216;name&#8217;)-&amp;gt;unique(); [&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-24831","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24831","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=24831"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24831\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}