Laravel 5自定义内置登录重定向

时间:2015-10-18 16:52:39

标签: laravel authentication laravel-5 laravel-5.1 laravel-middleware

我正在定制laravel 5的内置登录,以便根据我添加到users表的 type 列重定向到三个不同的路径,我尝试更改句柄功能RedirectIfAuthenticated中间件。但它似乎总能找到主URI。

这是我编辑过的中间件

public function handle($request, Closure $next)
    {
        if ($this->auth->check() && $this->auth->user()->type == 'patient') {
            // return redirect('/home');
            return 'PATIENT VIEW';
        } elseif ($this->auth->check() && $this->auth->user()->type == 'doctor') {

            return 'DOCTOR VIEW';

        } elseif ($this->auth->check() && $this->auth->user()->type == 'nurse') {

            return 'NURSE VIEW';
        }

        return $next($request);
    }

我是laravel 5的新手,我真的很感激任何帮助和解释

2 个答案:

答案 0 :(得分:1)

RedirectIfAuthenticated在这里被滥用了。该中间件用于经过身份验证的用户尝试访问仅应由guest虚拟机访问的页面时。例如,如果我是用户,并且我尝试查看登录或注册表单,那么它就不会让我。

我不会弄乱身份验证本身......其中一些很容易定制,但您尝试做的事情并非如此。我会先让Laravel对它们进行身份验证,然后再处理后续操作。

/home是用户登录时的默认路由。将if检查移动到该路由控制器方法。更好的是......如果你把事情做好,你根本不需要任何检查。

class HomeController {
    public function index()
    {
        $user = \Auth::user();

        return view($user->type . '.dashboard');
    }
}

现在您只需要名为patient/dashboard.blade.phpdoctor/dashboard.blade.php等的视图。如果您有更复杂的逻辑,那么您可能需要实际的重定向

        return redirect('home/' . $user->type);

为每种类型定义路线

Route::get('home/patient', 'PatientController@dashboard');
Route::get('home/doctor', 'DoctorController@dashboard');
Route::get('home/nurse', 'NurseController@dashboard');

然后在那些控制器方法中做任何你需要的事情。

答案 1 :(得分:0)

查看Authentication section

中的文档

基本上你需要的是:

  • app / Http / routes.php 创建身份验证路由:

    // Authentication routes...
    Route::get('auth/login', 'Auth\AuthController@getLogin');
    Route::post('auth/login', 'Auth\AuthController@postLogin');
    
  • 创建登录表单视图:

    <!-- resources/views/auth/login.blade.php -->
    <form method="POST" action="/auth/login">
    {!! csrf_field() !!}
    
    <div>
        Email
        <input type="email" name="email" value="{{ old('email') }}">
    </div>
    
    <div>
        Password
        <input type="password" name="password" id="password">
    </div>
    
    <div>
        <input type="checkbox" name="remember"> Remember Me
    </div>
    
    <div>
        <button type="submit">Login</button>
    </div>
    </form>
    
  • 手动验证用户 app / Http / Controllers / Auth / AuthController.php

    <?php
    
    namespace App\Http\Controllers;
    
    use Auth;
    use Illuminate\Routing\Controller;
    
    class AuthController extends Controller
    {
        /**
         * Handle an authentication attempt.
         *
         * @return Response
         */
        public function authenticate()
        {
            if (Auth::attempt(['email' => $email, 'password' => $password])) {
                if (Auth::user()->type == 'patient'){
                    return redirect()->intended('patientDashboard');
                }
                if (Auth::user()->type == 'doctor'){
                    return redirect()->intended('doctorDashboard');
                }
            }
        }
    }
    
  • 或者,如果您想将逻辑保留在RedirectIfAuthenticated中间件下,您可以修改代码:

    public function handle($request, Closure $next)
    {
    if ($this->auth->check())
    {
        //we have a logged user check if it's patient
        if($this->auth->user()->type == 'patient'){
    
          return new RedirectResponse(url('/patient'));
    
        }else if($this->auth->user()->type == 'doctor'){
    
          return new RedirectResponse(url('/doctor'));
    
        }
    }
    
    return $next($request);
    }
    

另外,您应该查看此Entrust包。