cook
  • Home
     
  • About Me
     

Redirecting to the proper page after actions

Jimmy Bourassa | November 29, 2008

A couple days ago, a client I currently work for found a little annoyance with cakePHP default’s behaviour of redirecting to the index action after a delete or edit action, thus ignoring the page you were on.

Indeed, redirecting to $this->referer wouldn’t work in this case because the data is posted to the same action, therefore $this->referer = $this->here. The solution is quite simple, add this code to your action:

if ($this->referer != $this->here)
	$this->Session->write('referer', $this->referer);

Then, if your save method is successful, you can safely redirect with the Session variable that’s just been set :

$this->redirect($this->Session->read('referer'));

It’s pretty simple, but I guess it might come in handy. Thanks to TommyO from the irc channel for this hint!

Comments
2 Comments »
Comments rss Comments rss

Recent Posts

  • OpenX and HostGator shared hosting
  • Common mistakes when working with accentuated characters
  • Model “X” with multiple associations leading to Model “Y”
  • Sort search results by relevance
  • Redirecting to the proper page after actions

People I like

  • Jean-Philippe Doyle
  • Knowland Design
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox