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.
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
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.
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.
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');
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With