2

我自定义laravel 5的内置登录,以便根据类型列将其重定向到三个不同的路径,我添加到users表中,我试着改变RedirectIfAuthenticated的句柄函数中间件。但它似乎总是找到家庭URI。Laravel 5自定义内置登录重定向

这里是我的编辑中间件

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,我会非常感谢所有帮助和解释

回答

1

RedirectIfAuthenticated在这里被滥用。该中间件适用于经过身份验证的用户尝试访问只应由访客访问的页面。例如,如果我是用户,并且尝试查看登录或注册表单,它不会让我。

我不会搞砸认证本身......它的一些很容易定制,但你想要做的不是。我只会让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', '[email protected]'); 
Route::get('home/doctor', '[email protected]'); 
Route::get('home/nurse', '[email protected]'); 

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

+0

非常感谢你:)这对我很有用。 –

0

查核在Authentication section

基本上你需要的是文档:

  • 创建auth路由a吨应用程序/ HTTP/routes.php文件

    ​​
  • 创建登录表单视图:

    <!-- 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> 
    
  • 手动验证用户应用程序/ HTTP /控制器/认证/ 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'); 
          } 
         } 
        } 
    } 
    
  • 或者如果你想保持RedirectIfAuthen下的逻辑ticated中间件你可能只是解决您的代码:

    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包。

+0

其仍在使用其内置..我不知道在哪里编辑它的内置函数 –