我的 Laravel 5.3 有三种不同类型的用户.我希望他们在登录后被重定向到不同的仪表板页面.例如:
I have Laravel 5.3 with three different types of users. I want them to be redirected to different dashboard pages after logging in. For example:
用户 ->登录 ->用户仪表板
user -> login -> user-dashboard
管理员 ->登录 ->管理仪表板
admin -> login -> admin-dashboard
我创建了一个名为 CheckRole 的中间件:
I have created a middleware called CheckRole:
public function handle($request, Closure $next)
{
if($request->user() === null) {
return response("Insufficient Permissions" , 401);
}
$actions = $request->route()->getAction();
$roles = isset($actions['roles']) ? $actions['roles'] : null;
if($request->user()->hasAnyRole($roles) || !$roles) {
return $next($request);
}
return response("Insufficient Permissions" , 401);
}
路线
Route::group(['middleware' => ['auth','roles'], 'roles' => 'Admin'], function () {
// Routes here
}
角色运行良好.
现在 redirectTo=''; 在 LoginContoller 中只指向一个视图.我检查了文档,我相信这与没有解释如何设置它的守卫有关.
Now redirectTo= ''; in the LoginContoller points to one view only. I have checked the documentation and I believe this has something to do with guards which have no explanation on how to set it up.
我也见过多重身份验证,但我认为为不同的用户创建不同的表并因此寻找替代答案是不明智的.
I have also seen multiauth, but I do not think it is wise to create different tables for different users and hence looking for an alternate answer.
任何建议将不胜感激.
我的表是这样的:
Table users
id | name | email
---------
1 | John | john@blah.com
2 | Michael | michael@blah.com
Table roles
id | name
---------
1 | Admin
2 | PrivilegedMember
3 | Subscriber
Table user_role
id | user_id | role_id
----------------------
1 | 1 | 1
2 | 2 | 2
这可能与以下问题重复,但提供的答案没有解释多次重定向.
This might be a duplicate of the below question but the answer provided leaves without explaining multiple redirections.
Laravel 5.3 中的多重身份验证
在你的 LoginController 中实现一个 authenticated() 方法并在那里添加重定向逻辑:
Implement an authenticated() method in your LoginController and add the redirection logic there:
<?php
namespace AppHttpControllersAuth;
use AppHttpControllersController;
use IlluminateFoundationAuthAuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
// ...
/**
* The user has been authenticated.
*
* @param IlluminateHttpRequest $request
* @param mixed $user
*
* @return mixed
*/
protected function authenticated(Request $request, $user)
{
if($user->hasRole('Admin')) {
return redirect()->intended('admin');
}
if ($user->hasRole('PrivilegedMember')) {
return redirect()->intended('PriviligedMember/index');
}
}
// ...
}
该方法在用户通过身份验证后调用.查看sendLoginResponse的最后两行:
The method is called after the user is authenticated. See the last two lines of sendLoginResponse:
/**
* Send the response after the user was authenticated.
*
* @param IlluminateHttpRequest $request
*
* @return IlluminateHttpResponse
*/
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
因此它是此类逻辑的完美候选.
So it's a perfect candidate for such logics.
关于您自己的答案的另一个注意事项,AuthenticatesUser 是水平扩展 LoginController 的特征,您可以安全地覆盖控制器中的任何方法,而无需触及核心文件.
One other note on your own answer, the AuthenticatesUser is a trait that horizontally extends the LoginController, you can safely override any of its methods in your controller without touching the core files.
这篇关于Laravel 5.3 登录重定向到多个用户的不同页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 Laravel 集合对象中添加新元素add new element in laravel collection object(在 Laravel 集合对象中添加新元素)
在 Laravel 5 中创建编辑模式Creating an edit modal in Laravel 5(在 Laravel 5 中创建编辑模式)
用于集合的 Laravel 5.5 API 资源(独立数据)Laravel 5.5 API resources for collections (standalone data)(用于集合的 Laravel 5.5 API 资源(独立数据))
在 php Laravel 5 中创建自定义辅助函数的最佳实践What is the best practice to create a custom helper function in php Laravel 5?(在 php Laravel 5 中创建自定义辅助函数的最佳实践是什么
没有“Access-Control-Allow-Origin"标头 - LaravelNo #39;Access-Control-Allow-Origin#39; header - Laravel(没有“Access-Control-Allow-Origin标头 - Laravel)
Laravel Passport Route 重定向到登录页面Laravel Passport Route redirects to login page(Laravel Passport Route 重定向到登录页面)