Laravel 5.3 LoginController - Header may not contain more than a single header, new line detected

左心房为你撑大大i 提交于 2020-01-11 09:03:19

问题


I have a problem when changing the default LoginController redirect after login, I'm getting an ErrorException in Response.php line 339: Header may not contain more than a single header, new line detected

I have already tried everything but it just does not work, the code is:

class LoginController extends Controller
{

protected $redirectTo = '/home';

protected function redirectTo()
{
    if (\Auth::check()) {
       $user_id = \Auth::id();
       $usuario = users::where('id','=',$user_id)->first();
       if($usuario->hasRole('copy')){
           return redirect('/copy/dashboardCopy');
        }
    } 
}

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}
}

According to Laravel documentation, the method has higher priority than the attribute, so I assume it's okay to leave the class attribute as it is.

And also, I have already checked, and the code is actually reaching the last condition.


回答1:


The method redirectTo should return an url path, not the Redirect response.

...
protected function redirectTo()
{
    if(\Auth::user()->hasRole('copy')){
        return '/copy/dashboardCopy';
    }       
}
...



回答2:


I have just solved it replacing the original code, with

class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo;

protected function redirectTo()
{
    if(\Auth::user()->hasRole('copy')){
        $this->redirectTo = '/copy/dashboardCopy';
        return $this->redirectTo;
    }       
}

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}
}



回答3:


Comment This Part in Authenticate.php Of app/Http/Middleware

protected function redirectTo($request)
         {
             if (! $request->expectsJson()) {
                return view('admin.auth.login');

             }
         }



回答4:


public $redirectTo = '/lender/home';

protected function redirectTo()
{
    if(\Auth::guard('lender')->check()){
      $this->redirectTo = '/lender/home';
      return $this->redirectTo;
    }
}


来源:https://stackoverflow.com/questions/42011025/laravel-5-3-logincontroller-header-may-not-contain-more-than-a-single-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!