Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 542
Aarón Gutiérrez Avatar asked Feb 02 '17 19:02

Aarón Gutiérrez


2 Answers

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

...
protected function redirectTo()
{
    if(\Auth::user()->hasRole('copy')){
        return '/copy/dashboardCopy';
    }       
}
...
like image 69
OuailB Avatar answered Oct 16 '22 21:10

OuailB


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']);
}
}
like image 20
Aarón Gutiérrez Avatar answered Oct 16 '22 21:10

Aarón Gutiérrez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!