Zend Framework 1 Ajax

Could somebody please suggest me a good beginner tutorial of using Ajax with Zend 1.I have been searching the net for some of this tutorials, but couldn't find an understandable one.In some they say you have to create a .json.phtml file for json response, the others don't.I am very confused about all these ajax calls with Zend Framework 1. Would be very grateful.

Well there really are some basic things.

  1. Get your data (from DB, file, in-code array, whatever)
  2. Get the controller helper
  3. Send the JSON response

And that's it. OK, not exactly but basically yes! Provided you have the data in $data:

$this->_helper->json($data, true);

will return a JSON response. The documentation is here.

Now there is the other notion of a Context Switch and AjaxContentHelper which:

The ContextSwitch action helper is intended for facilitating returning different response formats on request. The AjaxContext helper is a specialized version of ContextSwitch that facilitates returning responses to XmlHttpRequests.

To enable either one, you must provide hinting in your controller as to what actions can respond to which contexts. If an incoming request indicates a valid context for the given action, the helper will then:

  • Disable layouts, if enabled.
  • Set an alternate view suffix, effectively requiring a separate view script for the context.
  • Send appropriate response headers for the context desired.
  • Optionally, call specified callbacks to setup the context and/or perform post-processing.

Something like this:

$contextSwitch = $this->_helper->getHelper('contextSwitch');
$contextSwitch->setDefaultContext('json');
$contextSwitch->addActionContext('index', array('xml','json'))->initContext();
$contextSwitch->addActionContext('get', array('xml','json'))->initContext();
$contextSwitch->addActionContext('post', array('xml','json'))->initContext();
$contextSwitch->addActionContext('put', array('xml','json'))->initContext();
$contextSwitch->addActionContext('delete', array('xml','json'))->initContext();
$contextSwitch->addActionContext('head', array('xml','json'))->initContext();

You don't really need a tutorial I think. All you need is a good basic knowledge of how the web works internally and to read the Zend Documentation. Anyway here is some tutorial on ContextSwitch.