Laravel 5.3登录重定向到多个用户的不同页面 [英] Laravel 5.3 Login redirect to different pages for multiple users

查看:67
本文介绍了Laravel 5.3登录重定向到多个用户的不同页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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->登录-> admin-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
}

角色运转良好.

现在LoginContoller中的redirectTo= '';仅指向一个视图.我已经检查了文档,并相信这与警卫有关,警卫对此没有任何说明.

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.

我也看到了multiauth,但是我认为为不同的用户创建不同的表并因此寻找替代答案是不明智的.

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 App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;

    // ...

    /**
     * The user has been authenticated.
     *
     * @param  \Illuminate\Http\Request  $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  \Illuminate\Http\Request  $request
 *
 * @return \Illuminate\Http\Response
 */
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登录重定向到多个用户的不同页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆