2

I am trying to redirect to a new page using CakePHP redirect method but for some reason the redirect is not working. The code I am using to redirect is

public function update_sticky_postition_in_db()
{   
     $this->autoRender = false;

     ... // Save info to database

     $this->redirect(array('action' => 'tool'));
}

Where tool is the name of the view I am tring to redirect to. To try and speed the process of finding the problem I have checked few things and think I have found the cause of the problem. Basically I am trying to redirect to the view that is currently active which I think is part of the reason why it is not redirecting. I have read that it might have something to do with caching of the page but I am not sure how to solve that issue.

Also when using firebug I can see the redirect is sending a GET request but after that nothing is happening. Do I have to do something with the GET request or should Cake handle that for me. Also I have checked the URL of the GET and it is correct.

It is located within the controller with the correct name as I can view the original tool page.

Also the update_sticky_postition_in_db() method does not have a view (hence why the auto render is set to false), its intended purpose is to update a row in the database from an ajax call.

Lsakurifaisu
  • 133
  • 1
  • 12
  • Do you have this problem only in this page or in all pages? – Alessandro Minoccheri Sep 12 '12 at 09:07
  • It appears that it will not redirect to any page. Also I should add the redirect is not in the tool method of the controller. – Lsakurifaisu Sep 12 '12 at 09:14
  • 1
    can you show the context, where is $this->redirect... (I assume it's in controller yes?) – Greg Motyl Sep 12 '12 at 09:15
  • Post more of your code of your controller and your view. But you have to redirect into your controller not in other part – Alessandro Minoccheri Sep 12 '12 at 09:17
  • Ok I have updated it with the function call, which gets called as a GET request is sent. I have also tried commenting out the update the database code and leaving just the redirect and it still doesn't work. – Lsakurifaisu Sep 12 '12 at 09:24
  • Do I have to handle the GET request somewhere in my code? – Lsakurifaisu Sep 12 '12 at 09:34
  • What URL are you using to get to this controller action? Can you `exit('here')` in the action method to verify it is being called? – Dunhamzzz Sep 12 '12 at 09:54
  • I can exit before the redirect call, I am not sure after the call. It looks like the GET request is still waiting for a response. The URL the get request sends is localhost/domainName/AudienceEngagements/tool – Lsakurifaisu Sep 12 '12 at 09:59

1 Answers1

4

From your post it seems you're firing the update_sticky_postition_in_db() using ajax call, so that the redirection will not work.

You can do redirection using JavaScript within ajax success method.

In order to do that, you may send some json_encode() message from you above method and checking that status within ajax success method you can do a redirect using window.location.

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • Also would happen to know a good way to debug data using the method I am currently using. I have tried using the debug and set flash methods (part of the reason I was trying to refresh to page) but neither of those seem to work. Any ideas? – Lsakurifaisu Sep 12 '12 at 14:24
  • To answer my own comment about debugging the information, you can use the method $this->log(). Here is a link to the [documentation](http://book.cakephp.org/1.3/en/view/1195/Writing-to-logs). – Lsakurifaisu Sep 13 '12 at 10:29