-1

我在一個項目中,該網站有兩種類型的用戶管理員用戶。我做了一個中間件管理,則中間件工作正常,但是當我嘗試登錄的問題出現了:Laravel - 太多重定向問題

過多的重定向

路線

<?php 

Route::get('/', [ 
    'uses' => '[email protected]', 
    'as' => 'welcome' 
]); 
Route::get('/{form?}', [ 
    'uses' => '[email protected]', 
    'as' => 'index' 
]); 
Route::post('/exsist',[ 
    'uses' => '[email protected]', 
    'as' => 'login' 
]); 
Route::group(['middleware' => 'auth'],function(){ 
    Route::group(['prefix' => '/admin','middleware' => 'admin'],function(){ 
     Route::group(['prefix' => '/my'],function(){ 
      Route::get('/home', [ 
       'uses' => '[email protected]', 
       'as' => 'admin.home' 
      ]); 
     }); 
    }); 
    Route::group(['prefix' => '/'],function(){ 
     Route::get('/home', [ 
      'uses' => '[email protected]', 
      'as' => 'home' 
     ]); 
    }); 
}); 

and my 控制器

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Auth; 

use App\Http\Requests; 
use App\User; 
class UserController extends Controller 
{ 
     //main page 
    public function getIndex($form = ''){ 
      if(Auth::check()){ 
       if(Auth::user()->isAdmin()){ 
        return redirect()->route('admin.home'); 
       }else{ 
        return redirect()->route('home'); 
       } 
      } 
      return view('welcome', ['form' => $form]); 
     } 
     //main login (index page) 
     public function postLogin(Request $request){ 
      //validation 
      $this->validate($request,[ 
       'email' => 'required|email', 
       'pass' => 'required' 
      ]); 
      //authintecating user 
      if(Auth::attempt(['email'=>$request['email'], 'password'=>$request['pass']])){ 
       if(Auth::user()->isAdmin()){ 
        return redirect()->route('admin.home')->with(['success'=>'You have successfuly loged in as Admin!']); 
       }else{ 
        return redirect()->route('home')->with(['success'=>'You have successfuly loged in as User!']); 
       } 
      } 
      return redirect()->route('index')->with(['fail'=>'Wrong Email or Password!']); 
     } 
     public function getHome(){ 
      return view('home'); 
     } 
} 

爲什麼瀏覽器給我一個錯誤?

回答

2

嘗試改變:

Route::get('/{form?}', [ 
'uses' => '[email protected]', 
'as' => 'index' 
]); 

要:

Route::get('/form/{form?}', [ 
'uses' => '[email protected]', 
'as' => 'index' 
]); 

我相信當你到 '/' 路線,在正在執行的路線 '/ {形式?}'。

+0

男人,你是一個天才(y)..我很尷尬,我怎麼想到那個 –