Laravel-5登录后重定向到/home [英] Laravel-5 redirecting to /home after login

查看:101
本文介绍了Laravel-5登录后重定向到/home的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个具有Laravel-5.1角色的自定义身份验证系统.

I'm building a custom authentication system with roles in Laravel-5.1.

我有两组用户,主管和管理员.但是我只有一个登录页面.因此,当用户登录时,它首先对他们的凭据进行身份验证,然后编写了一个中间件来检查他们是哪种用户.

I have two sets of users, supervisors and administrators. But I only have one login page. So when the user logs in, it authenticates their credentials first, then I have written a middleware which checks which kind of user they are.

如果他们是管理员,则应将其重定向到/admin/home.如果他们是主管,则应将他们重定向到supervisor/home.

If they are an administrator, it should redirect them to /admin/home. If they are a supervisor it should redirect them to supervisor/home.

我可以从源文件Illuminate/Foundation/Auth/RedirectsUsers.php中看到默认重定向是到/home.但是,如何更改它使其变得动态呢?

I can see from the source file Illuminate/Foundation/Auth/RedirectsUsers.php that the default redirect is to /home. However, how can I change that so it becomes dynamic?

我的路线文件:

/* Administrator Routes */

Route::group(['namespace' => 'Admin', 'middleware' => 'timesheets.admin:1'], function()
{
    return Route::get('admin/home', 'AdminController@index');
});

/* Supervisor Routes */

Route::group(['namespace' => 'Supervisor', 'middleware' => 'timesheets.supervisor:2'], function()
{
    return Route::get('supervisor/home', 'SupervisorController@index');
});

我的中间件文件:

<?php

namespace App\Http\Middleware\Auth;

use Closure, App;
use Illuminate\Contracts\Auth\Guard;

class AdminAuthenticate
{
    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $role)
    {
        if ($this->auth->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('login');
            }
        }

        /* Get user id */
        $userId = $this->auth->user()->userId();

        if (auth()->check() && auth()->user()->hasRole($userId, $role)) {
            return $next($request);
        }
        else
        {
            App::abort(403);
        }

    }
}

推荐答案

首先,在AuthController中设置$redirectPath,例如:

Firstly, set the $redirectPath in your AuthController like:

$redirectPath = 'Path/to/standard/user/on/authentication'

创建一个名为Admin的新中间件,如下所示:

Create a new middleware called Admin that would look like this:

public function handle($request, Closure $next)
{
    if ( Auth::check() && Auth::user()->isAdmin() )
    {
        return redirect('path/to/admin');
    }

    return $next($request);
}

您将需要在用户模型中创建一个新方法来检查管理员.

You will need to create a new method within your user Model to check for admin.

像这样:

public function isAdmin()
{
    return $this->admin;
}

这将返回用户表中admin字段的值.

This would return the value of your admin field in your users table.

然后在SupervisorController __construct()上,您可以调用管理中间件:

Then on your SupervisorController __construct() you could call your admin middleware:

public function __construct()
{

    $this->middleware('admin');

}

这将使管理员登陆到标准登录名,但是几乎立即将他们重定向到管理员主页.

This will make the admin land at the standard login, but redirect them to the admin home almost immediately.

这篇关于Laravel-5登录后重定向到/home的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆