1.项目目录下composer.json添加:
"require": {
"php": ">=7.1.3",
"fideloper/proxy": "~4.0",
"laravel/framework": "5.6.*",
"laravel/tinker": "~1.0",
"zizaco/entrust": "5.2.x-dev"
},
运行:composer update;
2.在config/app.php and add the following to the providers array:
Zizaco\Entrust\EntrustServiceProvider::class,
3.在config/app.php and add the following to the aliases array:
'Entrust'=>Zizaco\Entrust\EntrustFacade::class,
运行:php artisan vendor:publish
4.Open your config/auth.php and add the following to it:
'providers'=>[
'users'=>[
'driver'=>'eloquent',
'model'=>App\User::class,
'table'=>'users',
],
],
5.把下面添加到routeMiddleware array in app/Http/Kernel.php中:
'role'=>\Zizaco\Entrust\Middleware\EntrustRole::class,
'permission'=>\Zizaco\Entrust\Middleware\EntrustPermission::class,
'ability'=>\Zizaco\Entrust\Middleware\EntrustAbility::class,
运行:php artisan entrust:migration
php artisan migrate
6.创建模型app/Role.php,内容:
namespace App;
use Zizaco\Entrust\EntrustRole;
class Role extends EntrustRole
{
}
7.创建模型app/Permission.php,内容:
namespace App;
use Zizaco\Entrust\EntrustRole;
class Permission extends EntrustRole
{
}
8.在User中做如下修改:
< ?php
useZizaco\Entrust\Traits\EntrustUserTrait;//新添加
class User extends Eloquent {
use EntrustUserTrait; //add this trait to your user model... 新引用
}
9.创建迁移:
运行:php artisan make:seeder PermissionTableSeeder;
写入一下内容:
use Illuminate\Database\Seeder;
use App\Permission;
use App\Role;
use App\User;
class PermissionTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
//清空权限相关的数据表
Permission::truncate();
Role::truncate();
User::truncate();
DB::table('role_user')->delete();
DB::table('permission_role')->delete();
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
//创建初始的管理员用户,if necessary
$pilishen = User::create([
'name' =>'pilishen',
'email' =>'[email protected]',
'password' =>bcrypt('secret'),
]);
//创建初始的role(初始的角色设定)
$admin = Role::create([
'name' => 'admin',
'display_name' => '超级管理员',
'description' => 'super admin role',
]);
//创建相应的初始权限
$manage_user = Permission::create([
'name' =>'manage_user',
'display_name' => '用户管理',
'description' => '管理用户的权限'
]);
//给角色赋予相应的权限
$admin->attachPermission($manage_user);
//给用户赋予相应的角色
$pilishen->attachRole($admin);
}
}
10.在DatabaseSeeder添加:
public function run()
{
$this->call(PermissionTableSeeder::class);//新添加
}
运行:composer dump-autoload
php artisan db:seed