views:

327

answers:

2

In my controllers init() method I call the AjaxContext-Helper and set it for some methods/actions in that controller. Like so:

public function init()
{
    $ajaxContext = $this->_helper->getHelper('AjaxContext');
    $ajaxContext->addActionContext('setlabel', 'html');
    $ajaxContext->initContext();
}

Now when I want to use redirect from an ajax-request to a non-ajax method...

$this->redirector->gotoSimple('manage', 'team', null, array('id' => $teamID));

I have the problem that the page doesn't load. I assume that's because the AjaxContext messes up the headers.

I tried clearHeaders and clearContexts before redirecting but nothing works.

[edit] in the respective ajaxed method (AjaxContent) I normally responde with an ajax response to an myviewscript.ajax.phtml. But in a certain case I now want to redirect to a normal non-ajax action in another controller.[/edit]

Any ideas? Thanks!

+3  A: 

Ok. Hopefully I understand your goal.

First of all, there is no redirector method directly on an instance of Zend_Controller_Action.

So, this:

$this->redirector->gotoSimple('manage', 'team', null, array('id' => $teamID));

Should be this:

$this->_helper->redirector->gotoSimple('manage', 'team', null, array('id' => $teamID));

Unless, of course, you've assigned the redirector to a property of the controller. In which case, you should be right, but I'd double check that.

Also, calling the _redirect method, or the Redirector action helper during an XHR request will only redirect the XHR request itself. Are you trying to redirect the XHR request? Or actually redirect the user's browser to a different page?

Edit:

All right. So, w/r/t your comment: Your goal is to redirect the user's browser session to a different page from an XHR request, if certain conditions are met. The Redirector action helper will not be of use here. There is nothing special about an XHR/Ajax request... if it sees a 301/302 redirect, it willingly follows it, just like a regular browser request. It does not do any funny business.

So, to solve your problem, you'll have to do something a little custom. Here's something that would work:

  1. During the Ajax request, if the conditions for a user redirect are met, send some sort of special response back to the client; it could be whatever you want.. a particular string, a JSON response... anything that you can easily check.
  2. When you receive the response on the client side, check it to see if it is that special message you sent in Step 1.
  3. If it is, you'll have to preform the redirect using Javascript. window.location = whatever will work just fine.
jason
I have a private $redirector and in the init() I assign $this->_helper->getHelper('Redirector'); so that should be ok. But if you say that I'm just redirecting the XHR request then that must be the problem. Indeed I just want to redirect the user.
tharkun
Note also that the URL you want to redirect to could be part of that response.
mercator
Thanks, I learned a few things about XHR now! On the basis of your approach I found a solution which is even easier. See my posted answer.
tharkun
A: 

On the basis of jasons approach I solved the problem by responding with a javascript redirect. So Instead of my usual message I send the javascript redirect to the myview.ajax.phtml:

$url = $this->view->url(array('controller' => 'team', 
                              'action' => 'manage', 
                              'id' => $teamID), null, null);

$this->view->response = '<script type="text/javascript">
                           window.location = "'.$url.'"
                        </script>';
tharkun