Redirect after register in Laravel

shaNnex picture shaNnex · Jun 28, 2015 · Viewed 9.9k times · Source

I am using Laravel 5.

I wanted to redirect a user after a successful registration. I have tried these in my authcontroller but it doesn't work.

protected $loginPath = '/plan';
protected $redirectTo = '/plan';
protected $redirectAfterRegister = 'plan/';

These work on successful login though but not after registration.

I have also tried postRegister to render a view but using a postRegister method overrides the registration process and I don't want to do that. I just want to redirect the user to a page on successful registration.

Answer

Francesco de Guytenaere picture Francesco de Guytenaere · Jun 28, 2015

Overriding the postRegister function like you mention should work, you would do this in your AuthController:

 public function postRegister(Request $request)
 {
    $validator = $this->registrar->validator($request->all());
    if ($validator->fails())
    {
        $this->throwValidationException(
            $request, $validator
        );
    }
    $this->auth->login($this->registrar->create($request->all()));     
    // Now you can redirect!
    return redirect('/plan');
 }

Or something like it. Copy it from the AuthenticatesAndRegistersUsers that is used in the top of your AuthController, here you will find all the functions

For this to work your AuthController should use the 'AuthenticatesAndRegistersUsers' trait, which is there by default.

More info about redirects in case you want to redirect to a named route: http://laravel.com/docs/5.0/responses#redirects