Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CashForCars's avatar

url()->previous() on page that POSTs to itself

Has anyone come up with a good solution of how to get the previous URL to a page that does a POST to itself to save form data. For example a settings page that is linked to from several pages.

As of now the only thing I can think of is storing the previous page of any page that needs this in the session, which will end up storing a huge amount of info.

I already dug into the source and didn't see any solutions there either. The issue is after doing a POST, and redirecting back to the same url, url()->previous() becomes the current URL (as it should based on how it works).

0 likes
8 replies
jlrdw's avatar

Do in sections, forma, formb, etc. The session can handle it.

Also pre validate on page, perhaps using js.

Of course validate on back end as needed.

Snapey's avatar

what's wrong with using return back() ?

just make sure you don't break the golden rule - always return a redirect from a post route and never a view.

1 like
CashForCars's avatar

Thanks for the replies, here's a bit more color:

My goal here was to make it as a reusable blade directive, like @back that just generically can remember for any given page, how the user entered that page, even if that page did a POST to itself to save data.

For example, lets say:

The page /user/1 links to /user/1/settings

Upon landing on /user/1/settings, url()->previous() its now /user/1 - great.

But after /user/1/settings does a POST to itself, /user/1/settings with the data, and redirects back to itself after saving, url()->previous() is now /user/1/settings, which is technically correct, since that was the previous page, but from a navigation standpoint, it's not what I'd want it to do. I'd prefer it be capable of knowing that navigation originally came from /user/1 and navigate back to that.

Snapey's avatar
Snapey
Best Answer
Level 122

put the return URL in the url as a url encoded parameter

/user/1/settings?from=%2Fuser%2F1

then make sure this is persisted in the form post and the redirect

on the first visit to the settings function, if it is not set, set it using the previous url

1 like
dcranmer's avatar

How about saving that initial url()->previous() to a variable or hidden form field and post it with the form?

CashForCars's avatar

Hey guys thanks for the ideas. The URL method is definitely the most ideal way, but that requires updating all posts to include that in the URL. I am using this back button in about 50 places, so just want to create a one-size-fits-all solution.

Here's what I ended up with:

Blade::directive('back', function () {
    return "<?php echo '<a href=\"' . session('b-' . url()->current(), url()->previous()) .'\">Back</a>'; (url()->previous() !== url()->current() && session(['b-' . url()->current() => url()->previous()])) ?>";
});

I would not recommend this for every use case, as it will potentially add a huge amount of records to session. The trade-off in my particular use case is worth it, but I think most would be better off with the URL based solution.

EDIT: Another reason not to use this as pointed out by Snapey is it can cause unexpected navigation if the page is loaded from a different tab and from a different entry point.

Snapey's avatar

i wouldn't recommend using session if the user is likely to use more than one tab as the session is shared between tabs

how many places you use the back button isn't really important, its the number of controller methods you would need to change that counts

CashForCars's avatar

Agreed, could cause awkward navigation in many cases. The usefulness of my solution is pretty limited, and should not be used except in limited use cases.

Please or to participate in this conversation.