第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Laravel 7:重定向到不同守衛(wèi)的不同登錄

Laravel 7:重定向到不同守衛(wèi)的不同登錄

PHP
郎朗坤 2023-07-30 13:15:47
我正在使用身份驗(yàn)證中間件并創(chuàng)建管理防護(hù)來(lái)控制管理訪問(wèn)。當(dāng)我嘗試訪問(wèn)管理路由時(shí)遇到一些問(wèn)題,我想將與管理路由關(guān)聯(lián)的未經(jīng)身份驗(yàn)證的流量重定向到 /admin/login 頁(yè)面,但它沒(méi)有這樣做,而是將我重定向到 /login 頁(yè)面。我不知道如何獲取與身份驗(yàn)證類中的路由關(guān)聯(lián)的守衛(wèi)。 protected function redirectTo($request)    {        if (! $request->expectsJson()) {            return route('login');        }    }}這就是代碼,我希望是這樣的: protected function redirectTo($request)    {        if (! $request->expectsJson()) {            if(Auth::guard('admin'))               return route('admin.login);            else               return route(login);        }    }}但它不起作用,因?yàn)槲椅ㄒ坏膮?shù)是 $request。這些是我的路線...//Admin RoutesRoute::middleware(['auth:admin'])->group(function () {Route::get('/admin', 'AdminController@index')->name('admin');Route::get('/newDoncente', 'AdminController@addDocenteView')->name('newDocente');//Docentes RoutesRoute::get('/docentes', 'Docente\DocenteController@getDocentesView')->name('getDocentesView');Route::get('/editDocente/{id}', 'Docente\DocenteController@editDocenteView')->name('editDocentesView');Route::get('/docentesTables', 'Docente\DocenteController@getDocentesDatatables')->name('getDocentesTables');Route::get('/docente/{id}', 'Docente\DocenteController@getDocenteView')->name('getDocenteView');謝謝。
查看完整描述

4 回答

?
RISEBY

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊

我一直在尋找基于守衛(wèi)的答案,我希望檢查守衛(wèi)而不是基于守衛(wèi)類型的重定向。但是我找不到解決方案。


然而,我找到了另一個(gè)可以完成工作的解決方案,但它并不像我正在尋找的答案那么動(dòng)態(tài)。然而,它對(duì)我來(lái)說(shuō)工作得很好



應(yīng)用\Http\Middleware\Authinticate.php


類中,添加以下代碼:


protected function redirectTo($request)

    {

        if (! $request->expectsJson()) {

            if($request->is('admin') || $request->is('admin/*'))

            return route('admin.login');

            

            return route('login');

        }

    }

另外,不要忘記為所有管理路由添加前綴 admin/ 。希望這個(gè)答案對(duì)您有幫助。


查看完整回答
反對(duì) 回復(fù) 2023-07-30
?
絕地?zé)o雙

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊

轉(zhuǎn)到應(yīng)用程序/Exception/Handler.php


使用這個(gè)類


use Illuminate\Auth\AuthenticationException;

并在類中添加這個(gè)函數(shù)



    protected function unauthenticated($request, AuthenticationException $exception)

    {

        if (request()->expectsJson()) {

            return Response()->json(['error' => 'UnAuthorized'], 401); //exeption for api

        }


        $guard = data_get($exception->guards(), 0);

        switch ($guard) {

            case 'admin':

                $login = 'admin.login';

                break;

            default:

                $login = 'login';

                break;

        }

        return redirect()->guest(route($login));

    }


查看完整回答
反對(duì) 回復(fù) 2023-07-30
?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

我不確定這是否是正確的方法,我重寫了handle方法,這樣我就可以從中獲取守衛(wèi),如下所示:


protected $guards = [];


public function handle($request, Closure $next, ...$guards)

{

    $this->guards = $guards;


    return parent::handle($request, $next, ...$guards);

}

protected function redirectTo($request)

{

    if (in_array("admin", $this->guards)) {

        return "admin/login";

    }


}


查看完整回答
反對(duì) 回復(fù) 2023-07-30
?
www說(shuō)

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊

好的,我已經(jīng)解決了,我想這是一個(gè)很好的方法。我創(chuàng)建了一個(gè)中間件,并要求中間件上的身份驗(yàn)證防護(hù)。


首先我創(chuàng)建中間件



class IsAdmin

{

    /**

     * Handle an incoming request.

     *

     * @param  \Illuminate\Http\Request  $request

     * @param  \Closure  $next

     * @return mixed

     */

    public function handle($request, Closure $next)

    {

    

        if (Auth::guard('admin')->check()) 

        return $next($request);

        else 

        return redirect('/admin/login');


    }

}


然后我將中間件放入內(nèi)核文件中


 protected $routeMiddleware = [

        'auth' => \App\Http\Middleware\Authenticate::class,

        'isAdmin' => \App\Http\Middleware\IsAdmin::class,

最后,我更改了路由的中間件


Route::get('/admin', 'AdminController@index')->name('admin');

現(xiàn)在它工作正常,但如果你有更好的解決方案,我真的很想知道。


查看完整回答
反對(duì) 回復(fù) 2023-07-30
  • 4 回答
  • 0 關(guān)注
  • 237 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)