views:

367

answers:

2

I have a controller setup that uses a whole bunch of different AjaxContent helpers. My init() for the controller looks something like this:

$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('index', 'html')
        ->addActionContext('compose', 'html')
        ->addActionContext('sent', 'html')
        ->addActionContext('recipients','html')
        ->addActionContext('inbox', 'html')
        ->addActionContext('sendsuccess','html')
        ->initContext();

At the end of the composeAction(), if a certain condition is met, the AJAX request should forward to sendsuccessAction().

Doing this with the standard _forward() method doesn't seem to forward it as an AjaxContent request - the page wants to render using the standard view template.

Any ideas on how I can use _forward or some other redirect method but keep the request as an AJAX request so the proper action context fires?

A: 

Use:

$this->_forward("action","controller","module",array('format'=>'html')); # or ajax or json or whatever
Rufinus
Tried that, no dice. Didn't seem to have an affect.
Andy Baird
You could just disable the layout in the sendsuccess action.
Rufinus
+1  A: 

Basically what's happening is that the initContext() method (which sets up the environment for an ajax response) only gets called for your first dispatched action, not the second.

There's a bunch of different ways around this.

First, to verify this is the issue, try calling

$this->_helper->ajaxContext->initContext();

from your sendsuccessAction. This will force the AjaxContext action helper to properly set up the viewRenderer again.

Alternatively, you could move the call to $ajaxContext->initContext() from the init() method to the preDispatch() method of your controller. This will cause it to run before each action is dispatched.

jason
Interesting - this worked but for whatever reason it requires me to load the standard view template "sendsuccess.phtml" instead of "sendsuccess.ajax.phtml". It does take care of disabling the layout for me though.
Andy Baird
To get around that, you may want to make sure that the `format` parameter does indeed get forwarded, like Rufinus suggested.
jason
Yes, I actually am doing that.$this->_forward('sentsuccess',null,null,array('format'=>'html'));
Andy Baird
That is weird. How the `ajaxContext` and `contextSwitch` helpers work, is simply by modifying the view script path specs (via inflector) in the `viewRenderer` helper. Those changes should (do, as far as I know) across dispatches. You must be manually doing something to the `viewRenderer` to override this behavior. Any calls to `viewRenderer` methods, or `$this->render()` from your forwarded to action may be suspect. See http://framework.zend.com/issues/browse/ZF-3447 for a similar issue.
jason