亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 7:重定向到不同守衛的不同登錄

Laravel 7:重定向到不同守衛的不同登錄

PHP
郎朗坤 2023-07-30 13:15:47
我正在使用身份驗證中間件并創建管理防護來控制管理訪問。當我嘗試訪問管理路由時遇到一些問題,我想將與管理路由關聯的未經身份驗證的流量重定向到 /admin/login 頁面,但它沒有這樣做,而是將我重定向到 /login 頁面。我不知道如何獲取與身份驗證類中的路由關聯的守衛。 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);        }    }}但它不起作用,因為我唯一的參數是 $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貢獻1856條經驗 獲得超5個贊

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


然而,我找到了另一個可以完成工作的解決方案,但它并不像我正在尋找的答案那么動態。然而,它對我來說工作得很好



應用\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/ 。希望這個答案對您有幫助。


查看完整回答
反對 回復 2023-07-30
?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

轉到應用程序/Exception/Handler.php


使用這個類


use Illuminate\Auth\AuthenticationException;

并在類中添加這個函數



    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));

    }


查看完整回答
反對 回復 2023-07-30
?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

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


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";

    }


}


查看完整回答
反對 回復 2023-07-30
?
www說

TA貢獻1775條經驗 獲得超8個贊

好的,我已經解決了,我想這是一個很好的方法。我創建了一個中間件,并要求中間件上的身份驗證防護。


首先我創建中間件



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');


    }

}


然后我將中間件放入內核文件中


 protected $routeMiddleware = [

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

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

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


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

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


查看完整回答
反對 回復 2023-07-30
  • 4 回答
  • 0 關注
  • 250 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號