3

I am building an app with Laravel 5.8 where, after registration or login, the user is redirected to a custom page along with a flashed session data, displayed on the page, that says "Welcome!".

I noticed that default redirect behavior in the RegisterController is a simple string, that does not allow me to add my custom redirect.

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

I tried modifying this default behavior replacing the string with a function:

protected function redirectTo()
{
    /* generate URL dynamically */
     return redirect('/custompage')->with('status', 'Welcome!');
}

But I get the Warning

ErrorException (E_WARNING) Header may not contain more than a single header, new line detected

So, how to redirect to the custom page AND add my custom flashed data? Of course without modifying any vendor code.

Thanks in advance

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
Enrico
  • 787
  • 12
  • 28

3 Answers3

3

change this to

protected function redirectTo()
{
    /* generate URL dynamicaly */
     return '/custompage';
}

It only returns path not the and you do not need redirect() here.

and added session data using Session::flash() or Session::put() depending on your requirement.

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
1

You can achieve what you described in different ways. A simple way will be to use the url of your custom route in the RegisterController, then add that route to your route and define a controller function accordingly.

You will keep your RegisterController like this:

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

Then add a route for it:

Route::any('custompage', array('as' => 'custompage', 'uses' => 'HomeController@custompage'));

And Define a controller function as you desire.

Elisha Senoo
  • 3,489
  • 2
  • 22
  • 29
  • Problem is I already handled the Route in my web.php file since I have a Route::resource(); when I add another one it throws an error that I redirect too many times – Enrico Apr 24 '19 at 09:41
1

You can do that in the redirectTo method. This method should return a string not a response object. So it should be like this

protected function redirectTo()
{
    /* flash data to the session here */
    session(['status', 'Welcome']);
     return '/custompage';
 }