Ronser
Ronser

Reputation: 1875

url::previous issue in laravel

I have a main list view page and a product page and an items page

I have used the URL::previous() in a a href="" tag

<a class="btn" title="Back" alt="Back" href="{{URL::previous()}}">Back</a>  

In my product page and in items page

When I go to products page through list view page and press the back button it goes to the pagged url of the list view page and when I press back button in items page it goes to the product page

But if I press the back button now it again goes to the items page instead of the list view page

Is there a way that I could only go back if I am from certain url in laravel?? or is there any functions regarding this...

Hope I have explained my problem up to a certain extent.. awaiting for your suggestions regarding this...

Upvotes: 3

Views: 12977

Answers (1)

Matt Burrow
Matt Burrow

Reputation: 11067

This is because the URL::previous() uses the HTTP_REFERER within the header, meaning the previous page is always the page you were on before.

And sometimes the HTTP_REFERER header does not always exist.

Either define the route you want to go back to using link_to_route() or a url generating method of your choice.

Or

You could use javascript and use window.history.back() within the onclick attribute of your back button but again, you may not get the intended results.

My solution would be to explicitly define the back route/url.

Upvotes: 4

Related Questions