本文实现基于前面两篇博客
【整理】Laravel 5 使用Entrust包如何注册用户时自动添加角色
【整理】Laravel 5 根据用户类型自动跳转 重写redirectPath方法
,请自行查看,本人此处只贴核心代码:
App\Http\Controllers\Auth\AuthController.php:
修改默认注册用户角色:
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$role = Role::where('name', 'common')->first(); // 查找默认普通用户角色
// $user->roles()->attach($role->id);
$user->attachRole($role); // 默认用户注册为普通用户
return $user;
}
laravel\framework\src\Illuminate\Foundation\Auth\RedirectsUsers.php:
修改跳转方法:
public function redirectPath()
{
// Logic that determines where to send the user
if (Auth::user()->hasRole('common')) { // 普通用户跳转前台页面
return '/';
} elseif (Auth::user()->hasRole(['admin', 'superadmin'])) { // 管理员,超级管理员跳转后台
return '/admin';
}
return '/';
}
文章的脚注信息由WordPress的wp-posturl插件自动生成
微信扫一扫,打赏作者吧~


