数据库迁移:
https://blog.csdn.net/qq_38191191/article/details/81335242
1、安装
想要在 Laravel 中使用 Entrust,首先需要通过 Composer 来安装其依赖包,在项目根目录www:composer require zizaco/entrust 5.2.x-dev
这时如果报错http://repo.packagist.org could not be fully loaded, package information was loaded from the local cache and may be out of date
就在cmd中直接输入composer config -g repo.packagist composer https://packagist.phpcomposer.com
然后回到上一步。
安装完成后需要在 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,
2、配置
在配置文件 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
报错:参考(https://www.cnblogs.com/imzhi/p/laravel-entrust-fix.html)
$ php artisan entrust:migration
In BoundMethod.php line 135:
Method Zizaco\Entrust\MigrationCommand::handle() does not exist
解决:
在 app/Providers/AppServiceProvider.php 中 register() 方法中加上如下代码:
|
然后通过以下命令生成相应的数据表:
php artisan migrate
报错:(https://www.jianshu.com/p/7527e2d486c9)
$ php artisan migrate
In Connection.php line 664:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `roles` add unique `roles_n
ame_unique`(`name`))
In Connection.php line 458:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes
解决:
问题根源
MySql支持的utf8编码最大字符长度为3字节,如果遇到4字节的宽字符就会出现插入异常。三个字节UTF-8最大能编码的Unicode字符是0xffff,即Unicode中的基本多文种平面(BMP)。因而包括Emoji表情(Emoji是一种特殊的Unicode编码)在内的非基本多文种平面的Unicode字符都无法使用MySql的utf8字符集存储。
解决问题
升级MySql版本到5.5.3以上。
手动配置迁移命令migrate生成的默认字符串长度,在AppServiceProvider中调用Schema::defaultStringLength方法来实现配置:
public function boot()
{
//
Schema::defaultStringLength(191);
}
再执行
$ php artisan migrate
报错:
In AppServiceProvider.php line 16:
Class 'App\Providers\Schema' not found
解决办法:
在红框处加入代码
use Illuminate\Support\facades\schema;
最终会生成 4 张新表:
- roles —— 存储角色
- permissions —— 存储权限
- role_user —— 存储角色与用户之间的多对多关系
- permission_role —— 存储角色与权限之间的多对多关系
- 4、模型类
Role
我们需要创建 Role 模型类 app/Role.php 并编辑其内容如下:
<?php namespace App;
use Zizaco\Entrust\EntrustRole;
class Role extends EntrustRole
{
}
Role 模型拥有三个主要属性:
- name —— 角色的唯一名称,如 “admin”,“owner”,“employee” 等
- display_name —— 人类可读的角色名,例如 “后台管理员”、“作者”、“雇主” 等
- description —— 该角色的详细描述
- display_name 和 description 属性都是可选的,在数据库中的相应字段默认为空。
Permission
接下来创建 Permission 模型 app/Permission.php 并编辑其内容如下:
<?php namespace App;
use Zizaco\Entrust\EntrustPermission;
class Permission extends EntrustPermission
{
}
Permission 模型也有三个主要属性:
- name —— 权限的唯一名称,如 “create-post”,“edit-post” 等
- display_name —— 人类可读的权限名称,如 “发布文章”,“编辑文章” 等
- description —— 该权限的详细描述
User
接下来我们在 User 模型中使用 EntrustUserTrait:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
class User extends Authenticatable
{
use Notifiable;
use EntrustUserTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
这将会建立 User 与 Role 之间的关联关系:在 User 模型中添加 roles ()、hasRole ($name)、can ($permission) 以及 ability ($roles,$permissions,$options) 方法。
软删除
使用 Entrust 提供的迁移命令生成的关联关系表中默认使用了 onDelete ('cascade') 以便父级记录被删除后移除其对应的关联关系。如果你由于某种原因不能在数据库中使用级联删除,那么可以在 EntrustRole、EntrustPermission 类以及 HasRole trait 提供的事件监听器中手动删除关联表中的记录。如果模型使用了软删除,那么当不小心误删除数据时,事件监听器将不会删除关联表数据。不过,由于 Laravel 事件监听器的局限性,所以暂时无法区分是调用 delete () 还是 forceDelete (),基于这个原因,在你删除一个模型之前,必须手动删除所有关联数据(除非你的数据表使用了级联删除):
$role = Role::findOrFail(1); // 获取给定权限
// 正常删除
$role->delete();
// 强制删除
$role->users()->sync([]); // 删除关联数据
$role->perms()->sync([]); // 删除关联数据
$role->forceDelete(); // 不管透视表是否有级联删除都会生效