【问题标题】:How to validate routes if a user is admin or not?如果用户是管理员,如何验证路由?
【发布时间】:2020-02-05 14:01:48
【问题描述】:
//This is the middle ware
public function handle($request, Closure $next)
{

        if(auth()->user()->isAdmin())   //isAdmin is a function in the User model which checks if the user is admin or not
        {
            return redirect('/admin');
        } else {
            return redirect('/home');
        }

    return $next($request);
}
//I already registered this middleware in kernel as well as verifyUser

Route::middleware(['auth', 'verifyUser'])->group(function() { 

Route::get('/home', 'HomeController@index')->name('home');

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

Route::get('/users/profile', 'UserController@view')->name('users.view-profile');

Route::get('/users/edit_profile', 'UserController@edit')->name('users.edit-profile');
});

这里的主要问题是它在浏览器中显示此错误 页面未正确重定向

Firefox 检测到服务器正在以永远不会完成的方式重定向对该地址的请求。

此问题有时可能是由禁用或拒绝接受 cookie 引起的。

【问题讨论】:

    标签: laravel laravel-5 routes laravel-5.8


    【解决方案1】:

    您是在告诉 Laravel 将管理员重定向到 /admin,将非管理员重定向到 /home

    但是,您也将 /admin/home subject 设置为该中间件,因此当用户访问 /home 时,它会再次将他们重定向到 /home(并再次,一次又一次,永远)。

    您可能需要进行两项更改:

    • 一个新的中间件,应用于管理员路由,仅将非管理员重定向到这些路由之外。
    • 将您的主页/管理逻辑作为一次性的登录后步骤,而不是每次浏览量。请参阅身份验证文档的path customization section

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 2023-03-08
      相关资源
      最近更新 更多