Laravel 5.2 以管理员身份登录后重定向管理页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35048503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:07:57  来源:igfitidea点击:

Laravel 5.2 Redirect admin page after login as admin

laravel

提问by Toomas Unt

Laravel 5.2 has been out for some time now. Yes, it has new auth function which is very good. Specially for beginners. My question, How to check if user is admin and then redirect safely to admin/dashboard properly? I know one way is to use admin flag in database but can any of you show some example?

Laravel 5.2 已经发布一段时间了。是的,它具有新的身份验证功能,非常好。特别适合初学者。我的问题,如何检查用户是否是管理员,然后正确地安全地重定向到管理员/仪表板?我知道一种方法是在数据库中使用 admin 标志,但你们中的任何人都可以举出一些例子吗?

回答by NIMROD MAINA

go to AuthController.php and add this method

转到 AuthController.php 并添加此方法

where role is the user role as defined in the database.

其中 role 是数据库中定义的用户角色。

protected function authenticated($request,$user){
    if($user->role === 'admin'){
        return redirect()->intended('admin'); //redirect to admin panel
    }

    return redirect()->intended('/'); //redirect to standard user homepage
}

回答by Ilario Engler

As in Laravel 5.3 / 5.4

就像在 Laravel 5.3 / 5.4 中一样

Add following line to create_users_table migration.

将以下行添加到 create_users_table 迁移。

$table->boolean('is_admin');

Add following method to LoginController.

将以下方法添加到 LoginController。

protected function authenticated(Request $request, $user)
{
    if ( $user->is_admin ) {
        return redirect('/admin/home');
    }

    return redirect('/home');
}

Additional note don't forget to add the following lines to RedirectIfAuthenticated middleware:

附加说明不要忘记将以下行添加到 RedirectIfAuthenticated 中间件:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        // the following 3 lines
        if (Auth::user()->is_admin) { 
            return redirect('/admin/home');
        }

        return redirect('/home');
    }

    return $next($request);
}

Otherwise if you e.g. type yourdomain/login and your logged in as admin it would be redirected to home instead of admin/home.

否则,如果您例如键入 yourdomain/login 并且您以管理员身份登录,它将被重定向到 home 而不是 admin/home。

回答by subzeta

AuthController extends the AuthenticatesAndRegistersUsers trait, which has a public method named redirectPath. In my case I would extend that method on the AuthController and I'd put my logic there:

AuthController 扩展了 AuthenticatesAndRegistersUsers trait,它有一个名为 redirectPath 的公共方法。在我的情况下,我会在 AuthController 上扩展该方法,并将我的逻辑放在那里:

public function redirectPath()
{
    if (Auth::user->myMethodToCheckIfUserHasAnAdminRoleLikeAnEmailOrSomethingLikeThat()) {
        redirect('admin/dashboard');
    }

    redirect('home');
}

回答by Mouhamad Hamadani

in Auth/LoginControllerthere is protected $redirectTo = '/home';change '/home'to '/loginin'for example, and create a new controller and in the controller get the information of the user and check if he is a admin or not and then redirect him to the proper page

认证/ LoginController中protected $redirectTo = '/home';变化的一个“/ home”“/ loginin”例如,创建一个新的控制器和控制获取用户的信息,并检查他是否是管理员或不是,然后重定向他到适当的页