想要在Laravel中使用Entrust,首先需要通过Composer
来安装其依赖包:
composer require zizaco/entrust 5.2.x-de
安装完成后需要在config/app.php
中注册服务提供者到providers数组:
Zizaco\Entrust\EntrustServiceProvider::class,
同时在该配置文件中注册相应门面到aliases数组:
'Entrust' => Zizaco\Entrust\EntrustFacade::class,
如果你想要使用中间件(要求Laravel 5.1或更高版本)还需要添加如下代码到 app/Http/Kernel.php 的 routeMiddleware
数组:
'role' => \Zizaco\Entrust\Middleware\EntrustRole::class, 'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class, 'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,
②配置
在配置文件config/auth.php
中设置合适的值,Entrust会使用这些配置值来选择相应的用户表和模型类:
'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, 'table' => 'users', ], ],
你还可以发布该扩展包的配置以便后续自定义相关表名以及模型类的命名空间:
php artisan vendor:publish
该命令会在 config 目录下创建一个 entrust.php 文件。
3、用户角色权限表
接下来我们使用Entrust提供的迁移命令生成迁移文件:
php artisan entrust:migration
如果执行上面的 命令出现 以下的错误:
处理方法: vendor-> zizaco-> entrust-> src-> commands-> MigrationCommand.php ,并将”fire“方法更改为”handle“ 然后通过以下命令生成相应的数据表:
php artisan migrate
最终会生成4张新表:
- roles —— 存储角色
- permissions —— 存储权限
- role_user —— 存储角色与用户之间的多对多关系
- permission_role —— 存储角色与权限之间的多对多关系
4、模型类
Role
我们需要创建Role模型类app/Role.php并编辑其内容如下:
<"htmlcode"><"htmlcode"><"htmlcode">$role = Role::findOrFail(1); // 获取给定权限 // 正常删除 $role->delete(); // 强制删除 $role->users()->sync([]); // 删除关联数据 $role->perms()->sync([]); // 删除关联数据 $role->forceDelete(); // 不管透视表是否有级联删除都会生效总结
无为清净楼资源网 Design By www.qnjia.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 《叶倩文 真心真意过一生》[WAV+CUE][400MB]
- 潘越云.1985-世间女子(滚石25周年经典复刻版)【滚石】【WAV+CUE】
- 陈柏宇.2009-CANT.BE.HALF【SONY】【WAV+CUE】
- 李明依.1989-小女生【滚石】【WAV+CUE】
- 《李荣浩 新时代创作新人王》[WAV分轨][310MB]
- 《黑鸭子与马头琴 史上最动听草原音乐》[WAV+CUE][320MB]
- 《汽车发烧音乐精选CD18》[FLAC+CUE][7.1G]
- 郑希怡.2004-Perfect.Date【英皇娱乐】【FLAC分轨】
- 群星.1994-宝丽金超级孖宝2CD【宝丽金】【WAV+CUE】
- 林忆莲.2019-0(2023精装版)【环球】【WAV+CUE】
- Supper.Moment.2016-The.Moment【寰亚】【FLAC分轨】
- 范莉丝.1979-送郎一朵牵牛花(LP版)【大联机构】【WAV+CUE】
- 刘宇.2024-宇宙诗人【哔哟哔哟文化】【FLAC分轨】
- 《全景声白噪音》沉浸式大自然助眠 带你听自然的声音[mp3]
- 纯音入心系列纯音乐《古筝精选 十指弹一赏心曲一心奏出世人心》1CD[MP3][582MB]