Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TheWizard's avatar

Header may not contain more than a single header, new line detected

Hi everyone. I'm trying to redirect my users after login, depending about their role (roles are in a column in users table).

(I didn't tell you but I'm using 5.4, sorry)

So, this is my LoginController (the built-in make:auth controllers and middleware, etc)

  use AuthenticatesUsers;

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

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


     protected function redirectTo()
    {   
        $UserId = Auth::id();

        if(Auth::check() && Auth::user()->role == 'Inquilino')
        {
            return redirect()->route('workspace',['id' => $UserId]);
        }
        elseif (Auth::check() && Auth::user()->role == 'Propietario') 
        {
            return redirect()->route('dashboard',['id' => $UserId]);
        }
    }

}  

& then, my Routes file

Route::group(['namespace' => 'Profiles'], function() {

    /*Landlords*/   

    Route::get('/workspace','LandlordController@workspace')->name('workspace'); 
    Route::get('/profile{id}','LandlordController@getProfile');
    Route::post('/profile','LandlordController@postProfile');


    /*Tenants*/

    Route::get('/tdashboard/{id}','TenantController@mainDashboard')->name('dashboard');
    Route::get('/bio{id}','TenantController@getProfile');
    Route::post('/bio','TenantController@postProfile');


});

The error is:


ErrorException in Response.php line 380: Header may not contain more than a single header, new line detected

    in Response.php line 380
    at HandleExceptions->handleError(2, 'Header may not contain more than a single header, new line detected', '/home/carlos/Documents/laravel54/newRo/vendor/symfony/http-foundation/Response.php', 380, array('this' => object(RedirectResponse), 'values' => array('http://localhost:8000/HTTP/1.0 302 Found Cache-Control: no-cache, private Location: http://localhost:8000/tdashboard/4 <!DOCTYPE html><html> <head> <meta charset="UTF-8" /> <meta http-e

I really need to redirect to these two specific routes because I need to show my "landlords" & "tenants" how much is their profile percentage complete.

What have I tried?

1.- overwrite the redirectPath method in trait RedirectUsers 2.- disable the middleware 3.- write my logic inside the middleware 4.- write my redirectTo method in LoginController above the constructor... 5.- Followed this https://laracasts.com/discuss/channels/general-discussion/laravel-5-redirect-to-url-after-registration?page=1

BUT the problem is that I'm calling two views at the same time & I don't know how am I doing this?

5.- Convert me in a monk an leave my country... :(

please, help! thank you!

0 likes
7 replies
TheWizard's avatar
TheWizard
OP
Best Answer
Level 8

I have fix the problem.

I have just need to redirect to strings, not using the route() method.

Thanks a lot! You have been very, very helpful. :)

6 likes
alirezavalipour93's avatar

you should write method like this

protected function redirectTo()
    {

        if (\Auth::user()->isAdmin())
            return 'admin/';
        else if (\Auth::user()->isProvider())
            return 'provider/';
        else
            return '/';
    }
7 likes
Virtualmix's avatar

I know it's an old resolved question but for anyone ending up here, it's absolutely possible to use the route() method. In @thewizard example, the redirect was causing the error, not the route() method.

Here is a working snippet:

protected function redirectTo()
    {

        if (\Auth::user()->isAdmin())
            return route('workspace',['id' => $UserId]);
        else if (\Auth::user()->isProvider())
            return route('dashboard',['id' => $UserId]);
        else
            return '/';
    }
3 likes

Please or to participate in this conversation.