PHP学习心得


entrust RBAC 权限管理

<h1>entrust RBAC 权限管理</h1> <ul> <li>Entrust</li> </ul> <h2>安装</h2> <pre><code class="language-php">// Composer 安装依赖包 composer require zizaco/entrust 5.2.x-dev // config/app.php 注册服务提供者到providers数组 Zizaco\Entrust\EntrustServiceProvider::class, // config/app.php 注册相应门面到aliases数组 'Entrust' =&gt; Zizaco\Entrust\EntrustFacade::class, // 中间件(&gt;= Laravel 5.1 版本),app/Http/Kernel.php 的 routeMiddleware 数组 'role' =&gt; \Zizaco\Entrust\Middleware\EntrustRole::class, 'permission' =&gt; \Zizaco\Entrust\Middleware\EntrustPermission::class, 'ability' =&gt; \Zizaco\Entrust\Middleware\EntrustAbility::class,</code></pre> <h2>配置</h2> <pre><code class="language-php">// config/auth.php 设置RBAC 权限管理使用的用户表和模型类 'providers' =&gt; [ 'users' =&gt; [ 'driver' =&gt; 'eloquent', 'model' =&gt; App\User::class, 'table' =&gt; 'users', ], ],</code></pre> <ul> <li>发布该扩展包的配置,方便之后自定义相关表名和模型类的命名空间</li> </ul> <pre><code class="language-bash"># 进入项目目录 D:\phpstudy_pro\WWW\laravel8\blog56&gt;,输入命令 php artisan vendor:publish # 显示内容为: Which provider or tag's files would you like to publish?: [0] Publish files from all providers and tags listed below [1] Provider: Fideloper\Proxy\TrustedProxyServiceProvider [2] Provider: Illuminate\Mail\MailServiceProvider [3] Provider: Illuminate\Notifications\NotificationServiceProvider [4] Provider: Illuminate\Pagination\PaginationServiceProvider [5] Provider: Laravel\Tinker\TinkerServiceProvider [6] Provider: Zizaco\Entrust\EntrustServiceProvider # 输入 6,回车,显示 Copied File [\vendor\zizaco\entrust\src\config\config.php] To [\config\entrust.php] Publishing complete. # 在config目录下创建entrust.php文件</code></pre> <h2>用户角色权限表</h2> <ul> <li>使用Entrust提供的迁移命令,生成迁移文件</li> </ul> <pre><code class="language-bash">php artisan entrust:migration # entrust 迁移 # Tables: roles, role_user, permissions, permission_role A migration that creates 'roles', 'role_user', 'permissions', 'permission_role' tables will be created in database/migrations directory Proceed with the migration creation? [Yes|no] (yes/no) [yes]: # 继续迁移创建,回车,默认选择yes,显示创建成功 Creating migration... Migration successfully created! # 查看生成迁移文件 database/migrations 目录生成文件:2021_08_23_102956_entrust_setup_tables.php</code></pre> <ul> <li>出现报错解决方法 <a href="https://www.jianshu.com/p/7527e2d486c9">https://www.jianshu.com/p/7527e2d486c9</a></li> </ul> <pre><code class="language-bash"># 报错内容: Method Zizaco\Entrust\MigrationCommand::handle() does not exist MigrationCommand.php 文件中,不到handle()这个方法,把文件中的fire()改成handle() # 解决方法 vendor\zizaco\entrust\src\commands\MigrationCommand.php 文件中, fire() 改成 handle()</code></pre> <ul> <li>生成相应的数据表</li> </ul> <pre><code class="language-bash">php artisan migrate # 命令执行成功显示: Migrating: 2021_08_23_102956_entrust_setup_tables Migrated: 2021_08_23_102956_entrust_setup_tables # 到数据库 our_blog 中,查看生成的数据表为: `roles` 存储角色 `role_user` 存储权限 `permissions` 存储角色与用户之间的多对多关系 `permission_role` 存储角色与权限之间的多对多关系</code></pre> <h2>创建模型类</h2> <ul> <li>Role 模型类</li> </ul> <pre><code class="language-php">php artisan make:model Models/Role // 文件内容为: namespace App\Models; use Illuminate\Database\Eloquent\Model; class Role extends Model { // }</code></pre> <ul> <li>Permission 模型类</li> </ul> <pre><code class="language-php">php artisan make:model Models/Permission // 文件内容为: namespace App\Models; use Illuminate\Database\Eloquent\Model; class Permission extends Model { // }</code></pre> <ul> <li>User 模型类使用 EntrustUserTrait</li> </ul> <pre><code class="language-php">php artisan make:model Models/User // 文件内容为: namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { // }</code></pre>

页面列表

ITEM_HTML