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

Issue Type: Improvement Created: 2008-04-22T03:14:03.000+0000 Last Updated: 2012-08-31T08:40:35.000+0000 Status: Open Fix version(s): Reporter: Carlton Gibson (carlton) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Controller

Related issues: Attachments:


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

getActionName(); $context = $this->getActionContexts($action) //check in case multiple contexts DO exist if(count($context) === 1) { $format = $context[0]; } return parent::initContext($format); } {quote} ### Comments Posted by Matthew Weier O'Phinney (matthew) on 2008-05-09T11:17:03.000+0000 Scheduling for next minor release Posted by Matthew Weier O'Phinney (matthew) on 2008-11-05T11:44:25.000+0000 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. Posted by Carlton Gibson (carlton) on 2008-11-06T00:04:24.000+0000 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... :-) Posted by Wil Sinclair (wil) on 2008-12-04T15:20:38.000+0000 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. Posted by Regis Leroy (regilero) on 2009-11-02T04:51:39.000+0000 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.

Have you found an issue?

See the Overview section for more details.


© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.