tags:

views:

98

answers:

4

I have an action (view for example) in a controller that is called from multiple other actions in other controllers. How is the best way to create a "Back" button that will take me back to the page that got me here?

I've used named parameters like "back_controller" and "back_action" and that works fairly well but they get awkward when the page has a form that gets submitted. I have to be sure to pass those parameters as hidden fields or in the form url and then look for them after the form has been processed.

Is there some kind of stack or other solution that anyone else has come up with that handles this situation better? I see this problem in a lot of my projects and I've yet to come up with a good solution.

A: 

$_SERVER['HTTP_REFERER']

Ben Reisner
+1  A: 

I don't completely understand your question, but this may be helpful:

If you need to redirect to the referer page you can use:

   $this->redirect($this->referer());

http://book.cakephp.org/view/425/redirect

Rahil Sondhi
That will work for the first jump but if the screen has a form and submits to itself, I would like to remember the other screen that got us there. Please see my comment to my original post for an example that might make it more clear.
Dan Berlyoung
A: 

Check out my HistoryComponent

Usage is described on my blog

neilcrookes
+1  A: 

I don't believe in back buttons. That is a feature that the browser does quite well and you would be better off having buttons always taking you to specific destinations rather than back.

If you must have a back button, you could create a history stack in the session. When a page loads you just push that page on the history (you will want to make sure you don't push the same page on the stack multiple times). You could create URL like /back who's sole job is to redirect the user to the last page they were on.

Daniel Ice