Search code examples
phplaravellaravel-5laravel-5.8laravel-session

Laravel custom redirect after Registration with custom flashed session data


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


Solution

  • 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.