AjaxContext Action Helper Proposal
Matthew is really burning the midnight oil at the moment and has produced another proposal. This time, it is an action helper to integrate Ajax calls more easily into a ZF MVC application. I’ve shameless lifted the rest of the information in this post from the proposal.
Zend_Controller_Action_Helper_AjaxContext will detect a XmlHttpRequest and do the following:
- Disables layouts (if enabled)
- Determines if the current action should respond via AJAX
- If the action is “ajaxable”, it then determines the response format and:
- sets the appropriate HTTP response header
- changes the view script suffix to reflect the format
All that you would have to do is mark your action as “ajaxable” (what a word!) by creating a public member variable called $ajaxable in your Controller and calling the helper’s initContext() function in your preDispatch() like this:
class CountriesController extends Zend_Controller_Action
{
/**
* Actions that allow an AJAX context
*/
public $ajaxable = array('autocomplete');
public function preDispatch()
{
$this->_helper->ajaxContext()->initContext();
}
// rest of class continues...
}
In this case you would be setting up the autocomplete action of the countries controller to respond using JSON or XML if called via the browser’s XMLHttpRequest object. Hypothetically, in this case, it may be an autocomplete listing attached to a form element for selecting your country in a comment form.
Certainly, this action helper will replace some of the code I currently have in a Front Controller plugin that’s designed to handle this problem. My code is considerably less flexible mind you, so I’d even gain some power!
Go and read the proposal on the Wiki and add comments. Your comments will make the component better!

