I am working on cart in laravel 5.5. Whenever guests click on "Add to cart", i am redirecting to login. If they have account, they will login and redirecting to product info they have selected. Otherwise they are registering. I wanted to redirect to customer selected product after registration. For Login, this is working fine.. return redirect()->intended(); For Registration intended url not working..
Asked
Active
Viewed 1.1k times
9
-
Please put some code here – AddWeb Solution Pvt Ltd Nov 17 '17 at 07:46
-
have you tried anything? – Ritesh Khatri Nov 17 '17 at 07:47
-
put it in session fetch it after registering – Gaurav Gupta Nov 17 '17 at 07:50
-
I tried this after login.. return redirect()->intended(); It is working fine. But i don't know how to achieve this after registration. – Sridhar Nov 17 '17 at 07:51
-
I am new to laravel. Can you please sample code for this. @GauravGupta – Sridhar Nov 17 '17 at 07:54
-
Possible duplicate of [Laravel 5.4 redirection to custom url after login](https://stackoverflow.com/questions/42177044/laravel-5-4-redirection-to-custom-url-after-login) – Amarjit Singh Nov 17 '17 at 07:55
-
I am asking about custom dynamic url after registration only @AmarjitSingh – Sridhar Nov 17 '17 at 07:59
-
override the `redirectTo` method in `App\Http\Controllers\RegisterController` – ABDEL-RHMAN Nov 17 '17 at 08:05
-
Tried it, but I am not able to get that selected product URL. @ABDEL-RHMAN – Sridhar Nov 17 '17 at 08:08
-
alright, try to override `registered` method and use `return redirect()->intended();` – ABDEL-RHMAN Nov 17 '17 at 08:10
-
I tried this. But it is giving error like this. **Header may not contain more than a single header, new line detected** @ABDEL-RHMAN – Sridhar Nov 17 '17 at 08:18
-
@Sridhar i tried that and it worked for the `registered` method and gives the same error for the `redirectTo` method did you remvoe it? – ABDEL-RHMAN Nov 17 '17 at 08:28
-
I don't have registered method. Can you please share code of your registered method. @ABDEL-RHMAN – Sridhar Nov 17 '17 at 08:43
-
first remove the `redirectTo` methd then implement the `registered` method in `RegisterController` like so `public function registered(){return redirect()->intended();}` – ABDEL-RHMAN Nov 17 '17 at 08:50
-
1Awesome.. It is working.. Thank you so much.. @ABDEL-RHMAN – Sridhar Nov 17 '17 at 08:57
4 Answers
13
In Controllers/Auth/RegisterController
change protected $redirectTo = '/';
at line 30
For dynamic URL replace protected $redirectTo = '/';
with
protected function redirectTo()
{
/* generate URL dynamicaly */.
return '/path'; // return dynamicaly generated URL.
}
you can also use return redirect()->intended(/* default uri to redirect user goes here */);
The intended method on the redirector will redirect the user to the URL they were attempting to access before being intercepted by the authentication middleware. A fallback URI may be given to this method in case the intended destination is not available.

Amarjit Singh
- 2,068
- 19
- 52
-
2Thanks for reply. I wanted this redirect location to be dynamic based on product name clicked by guest after registration. – Sridhar Nov 17 '17 at 07:57
-
Just read Laravel documentation here https://laravel.com/docs/5.5/authentication – Amarjit Singh Nov 17 '17 at 08:14
-
6I tried this. But it is giving error like this. **Header may not contain more than a single header, new line detected** – Sridhar Nov 17 '17 at 08:17
4
Instead of
protected $redirectTo = '/home';
Your can use this method
protected function redirectTo()
{
$userName = Auth::user()->name;
//use your own route
return route('user.edit', compact('userName'));
}

Ridowan Ahmed
- 97
- 1
- 2
- 11
2
Use function registered(Request $request, $user)
as follows
protected function registered(Request $request, $user)
{
if ($redirect_to_selected_prodcut) {
return redirect('/order/product');
}
return redirect()->intended();
}

mwa91
- 3,949
- 1
- 13
- 10
0
Below the code that does the login add
return \Redirect::intended('/default-url-if-user-did-not-come-from-any-page');

john godstime
- 27
- 8