Pierce Gearhart
Pierce Gearhart

Reputation: 45

Laravel Redirect Back Issues

I am using Laravel 8.0 to create a web application. I have multiple pages that can only be seen if you are signed into an account. I have it setup so that once you go to the page, it uses a controller function to return the view, but before that it checks if you are logged in with a simple if statement. If you are logged in, it returns the view, else it redirects to login page. I am wanting to make it so after they login it redirects them back to the page they were trying to go before. I used return redirect()->back(); to do this, but it returns them too the login page as it thinks that is where it was before. I use a separate route and controller function to login them in via the form. If anyone could help, I would much appreciate it!

Upvotes: 0

Views: 756

Answers (2)

A.A Noman
A.A Noman

Reputation: 5270

You have to use following code. In your login function just add this

Session::put('previousUrl',url()->previous());
$previous_url = Session::get('previousUrl');

And after login authentication successful just call this

return Session::get('previousUrl'); 

Upvotes: 1

Localhousee
Localhousee

Reputation: 912

i prefer use this

redirect()->route('your route name')

because in logic, after submit form, it goes to another route which is /login with POST Request

So when u use this

redirect()->back()

it goes to /login with GET Request which is your login form

Upvotes: 0

Related Questions