我使用中间件来检查用户。这里有两个用户管理员和学生。这两个细节都存储在用户table.Now中。问题是,当我登录时,它重定向到管理仪表板页面,但是页面显示不工作,重定向太多次。中间件
public function handle($request, Closure $next)
{
if(Auth::User()->type == 1) {
return redirect('AdminDashboard');
} else {
redirect('StudentDashboard');
}
}
homeController
use App\Http\Middleware\checkUser;
public function __construct()
{
$this->middleware('auth');
$this->middleware('checkUser');
}
public function index()
{
if(!(Auth::User)){
return redirect('login');
}
}
web.php
Auth::routes();
Route::get('/','UserController@index');
Route::get('/home', 'HomeController@index')->name('home');
Route::get('AdminDashboard', 'DashboardController@index')->name('AdminDashboard');
Route::get('dashboard/videos', 'DashboardController@videos')->name('adminVideos');
Route::post('file-upload/upload', 'FileUploadController@upload')->name('upload');
Route::get('StudentDashboard', 'DashboardController@studentRegister')->name('StudentDashboard');
Route::post('/newRegister', 'UserController@newRegister')->name('newRegister');
DashboardController
use App\Http\Middleware\checkUser;
public function __construct()
{
$this->middleware('auth');
$this->middleware('checkUser');
}
public function index()
{
$students = Students::with('classes')->with('subclasses')->get();
return view('admin.dashboard',compact('students'));
}
public function studentRegister()
{
$classes = Classes::where('status', '1')->get();
$subclasses = SubClass::where('status', '1')->get();
return view('admin.studentRegister',compact('classes','subclasses'));
}
发布于 2020-04-30 05:20:59
应该添加您以检查控制器中的用户功能。因为当中间件重定向到管理仪表板时,就会再次调用它,这就是为什么它重定向到多次。如果您仍然想使用中间件,那么在中间件中返回视图。希望这对你会有帮助。
发布于 2020-04-30 05:28:13
当您重定向时,它会返回到同一个中间件,然后再次重定向&这将导致多个重定向。因此,与其在这两种情况下重定向,不如在正确的情况下使用return $next
,在错误的情况下使用重定向。情商:
public function handle($request, Closure $next)
{
if(Auth::User()->type == 1) {
return $next;
}
redirect('StudentDashboard');
}
https://stackoverflow.com/questions/61516272
复制相似问题