久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Laravel 5.3 登錄重定向到多個(gè)用戶的不同頁(yè)面

Laravel 5.3 Login redirect to different pages for multiple users(Laravel 5.3 登錄重定向到多個(gè)用戶的不同頁(yè)面)
本文介紹了Laravel 5.3 登錄重定向到多個(gè)用戶的不同頁(yè)面的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我的 Laravel 5.3 有三種不同類型的用戶.我希望他們?cè)诘卿浐蟊恢囟ㄏ虻讲煌膬x表板頁(yè)面.例如:

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

我創(chuàng)建了一個(gè)名為 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
}

角色運(yùn)行良好.

現(xiàn)在 redirectTo='';LoginContoller 中只指向一個(gè)視圖.我檢查了文檔,我相信這與沒(méi)有解釋如何設(shè)置它的守衛(wèi)有關(guān).

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.

我也見(jiàn)過(guò)多重身份驗(yàn)證,但我認(rèn)為為不同的用戶創(chuàng)建不同的表并因此尋找替代答案是不明智的.

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

這可能與以下問(wèn)題重復(fù),但提供的答案沒(méi)有解釋多次重定向.

This might be a duplicate of the below question but the answer provided leaves without explaining multiple redirections.

Laravel 5.3 中的多重身份驗(yàn)證

推薦答案

在你的 LoginController 中實(shí)現(xiàn)一個(gè) 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');
        }
    }

    // ...
}

該方法在用戶通過(guò)身份驗(yàn)證后調(diào)用.查看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.

關(guān)于您自己的答案的另一個(gè)注意事項(xiàng),AuthenticatesUser 是水平擴(kuò)展 LoginController 的特征,您可以安全地覆蓋控制器中的任何方法,而無(wú)需觸及核心文件.

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.

這篇關(guān)于Laravel 5.3 登錄重定向到多個(gè)用戶的不同頁(yè)面的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

add new element in laravel collection object(在 Laravel 集合對(duì)象中添加新元素)
Creating an edit modal in Laravel 5(在 Laravel 5 中創(chuàng)建編輯模式)
Laravel 5.5 API resources for collections (standalone data)(用于集合的 Laravel 5.5 API 資源(獨(dú)立數(shù)據(jù)))
What is the best practice to create a custom helper function in php Laravel 5?(在 php Laravel 5 中創(chuàng)建自定義輔助函數(shù)的最佳實(shí)踐是什么?)
No #39;Access-Control-Allow-Origin#39; header - Laravel(沒(méi)有“Access-Control-Allow-Origin標(biāo)頭 - Laravel)
Laravel Passport Route redirects to login page(Laravel Passport Route 重定向到登錄頁(yè)面)
主站蜘蛛池模板: 视频一区二区三区在线观看 | 欧美日韩国产一区二区三区不卡 | 日韩欧美国产精品综合嫩v 一区中文字幕 | 天堂成人国产精品一区 | 久久免费视频观看 | 国产成人a亚洲精品 | 看片91 | 九九99靖品 | 天堂一区二区三区四区 | 久久高清 | 国产一二区免费视频 | 日韩精品一区二区三区四区 | 成人福利视频 | 久久青视频 | 国产一区91精品张津瑜 | 日韩精品久久久 | 日韩亚洲视频 | 91麻豆精品国产91久久久资源速度 | 亚洲精品视频在线播放 | 亚洲视频在线观看免费 | 超碰人人艹 | 国产精品久久久久久久久污网站 | 在线观看免费av网 | 二区成人 | 欧美a在线 | 九九热精品视频 | 伊人二区 | 成人免费视频7777777 | 桃花av在线 | 一区欧美| 免费一区二区三区在线视频 | 国产小视频在线 | 中文字幕欧美一区 | 国产精品久久久久久久久久久久冷 | 人人看人人草 | 浮生影院免费观看中文版 | 日韩欧美在线一区 | 亚洲精品第一 | 日韩国产一区二区三区 | 日本精品视频 | 欧美一区中文字幕 |