ZF-3162: AjaxContext requires format parameter even when only one format is allowed (This is redundant.)


With the Action Helper, AjaxContext you often only provide one alternate context format. The switch to this context is triggered via the detection of the isXmlHttpRequest() member of the request object. However, currently one also has to supply a format parameter with the request. This seems redundant.

I copy a thread from the MVC list below. This shows that it is fairly trivial to extend AjaxContext to automatically check is there is just one context and if so use it.

My thought though is that it may be nice to work this feature into the base ContextSwitch class. The idea would be that the first context format given would be the default, which would be applied if no format string was given, whereas the later formats would require you to provide the format parameter.

{quote} On 16 Apr 2008, at 21:25, Taylor Barstow wrote:

On Apr 11, 2008, at 10:10 AM, Taylor Barstow wrote:

When using the AjaxContext helper, it seems odd to me that I have to add ?format=X to my URIs in order to trigger the context switch, especially if I have only specified one possible format.

Shouldn't the X-Requested-With: XMLHttpRequest header be enough? Is there any way that I can trigger the context switch without passing the format parameter when there is only one possible format?

To follow up on this, I looked further into it and you can actually make this happen yourself:

class MyController extends Zend_Controller_Action {

 public $ajaxable = array('index' => array('html'));

 public function init() {


BIG CAVEAT: You would only want to do this if you only have one context format for the entire controller.

I hope this helps someone else


Following on from this suggestion, I think the following should allow us to forgo specifying the format parameter whilst still allowing multiple contexts within a single controller.

(I wasn't able to find a feature request for this sort of change to AjaxContext. Do people think it is worth filing one?)

Regards, Carlton

<?php /** * Removes need to specify format param when providing unique ajax context. */ class My_Controller_Action_Helper_UniqueAjaxContext extends Zend_Controller_Action_Helper_AjaxContext { /** * Automatically initialise unique AjaxContext on a per action basis. */ public function initContext($format = null) { $action = $request->getActionName(); $context = $this->getActionContexts($action)

    //check in case multiple contexts DO exist
    if(count($context) === 1)
        $format = $context[0];
    return parent::initContext($format);

} {quote}


Scheduling for next minor release

Actually, it doesn't make sense to add this to ContextSwitch, as you should still be able to access the action directly, with no context switching. AjaxContext makes more sense, however, as it only kicks into action if isXmlHttpRequest() is true.

It was a long time ago that this came up but I think the idea was that it would be nice to accommodate future extensions of ContextSwitch, perhaps RestContext or SoapContext. If these were to kick in via something like a isSoapRequest() method then they could equally use the functionality if it were embedded in ContextSwitch.

(It's possible that this is a YAGNI suspect, at best... :-)

Matthew, could you please figure out how we should resolve this one and mark it as such? If you want to leave it for the community, feel free, but give some instruction in a comment on how to implement it.

I think this is redondant and not bug-free. I add some bugs with few jQuery plugins where the XmlHttpRequest wasn't set. having the format setted, and this gives an error response no view script found. I think AjaxRequest plugin should purpose an option: ->disableIsXmlHttpRequestCheck() Activating this option the format parameter would become the only necessary option On the contrary we could imagine ->ignoreFormatParameter() and activated the first (and only) AjaxContext defined for this action. And maybe throw an Exception if two contexts have been defined.