Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect after register in Laravel

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.

like image 939
shaNnex Avatar asked Jun 28 '15 17:06

shaNnex


5 Answers

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

like image 185
Francesco de Guytenaere Avatar answered Nov 11 '22 09:11

Francesco de Guytenaere


There are two options to specify where to redirect the user in the app/Http/Controllers/Auth/RegisterController.php

For a simple URL you can override this property.

protected $redirectTo = '/home';

If you have a more complicated logic than just one static URL, since Laravel 5.3 you can add a method in the same class RegisterController, with name redirectTo():

protected function redirectTo()
{
    if (auth()->user()->role_id == 1) {
        return '/admin';
    }
    return '/home';
}

The method behavior will override $redirectTo property value, even if the value is present.

like image 33
Davide Casiraghi Avatar answered Nov 11 '22 08:11

Davide Casiraghi


Simply add below line to AuthController class in Auth/AuthController.php

protected $redirectPath= '/plan';

Above redirect path will be used for successful login and successful register.

like image 39
Shankar Prakash G Avatar answered Nov 11 '22 09:11

Shankar Prakash G


You can also modify the return of register() in RegisterUsers.php:

public function register(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    Auth::guard($this->getGuard())->login($this->create($request->all()));
    // Originally the parameter is $this->redirectPath()
    return redirect()->to('/plans');
}
like image 3
FJ_ Avatar answered Nov 11 '22 08:11

FJ_


Here is laravel 5.4 solution

     /**
     * The user has been registered.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function registered(Request $request, $user)
    {

          //User register now here you can run any code you want 
          if (\Request::ajax()){

            return response()->json([
                'auth' => auth()->check(),
                'intended' => $this->redirectPath(),
            ]);
            exit();
        }
        return redirect($this->redirectPath());
    }

keep in mind register() Handle a registration request for the application. where registered() called when user created.

like image 2
Abdul Manan Avatar answered Nov 11 '22 07:11

Abdul Manan