Apr
17

5 Things: Working with Zend Framework and Ajax


Today I wanted to write about 5 awesome Zend Framework components/helpers that can help you when working with ajax.

isXmlHttpRequest()

This handy little method can eliminate the need to send extra parameters such as /ajax/true/ to let the controller know it’s an Ajax request. Most standard JavaScript frameworks (jQuery, YUI, Prototype) pass the X-Requested-With HTTP header with the value ‘XMLHttpRequest’ when doing an Ajax request. isXmlHttpRequest() basically just checks to see if that header is set and if it is, then execute the code that needs to be executed if it’s an Ajax call.

/**
* myAction from myController
*/
function myAction()
{
if  ($this->getRequest()->isXmlHttpRequest()) {
// do the handling of your ajax request
}
else {
// if it's not an ajax request then do regular handling here
}
}

JSON action helper

I was surprised when I found this action helper in the framework. The JSON helper is pretty slick. It does 5 things that usually I had to do myself.

  • It Disables layouts if currently enabled.
  • Disables the ViewRenderer if currently enabled.
  • Sets the ‘Content-Type’ response header to ‘application/json’.
  • Immediately returns the response, without waiting for the action to finish execution.
  • Array passed as argument will be returned as a json formatted response.

So if you use this helper along with the isXmlHttpRequest() method you can easily handle any Ajax request and return a response with minimum amount of programming.

/**
* myAction from myController
*/
function myAction()
{
if ($this->getRequest()->isXmlHttpRequest()) {
// do the handling of your ajax request

$myArrayofData = array('a','b','c');

//encode your data into JSON and send the response
$this->_helper->json($myArrayofData);
//nothing else will get executed after the line above

}
else {
// if it's not an ajax request then do regular handling here
}
}

AutoComplete

This helper is currently supported only by Dojo and Scriptaculous AJAX libraries, so if you are working with any of those libraries, Zend’s AutoComplete will make your life a little bit easier. AutoComplete will do the same actions as the JSON helper (disable layout, sent appropriate content type, etc…) but will do the extra step to make sure your data is formatted correctly to use with the auto complete feature of the previously mentioned libraries. Definitely check this helper out if you are working with auto complete functionality.
To read more and see examples click here.

ContextSwitch and AjaxContext

These last two are very similar, If I’m not mistaken AjaxContext extends ContextSwitch to contain all the functionality of ContextSwitch, but, there are a couple of key differences that you must know! ContextSwitch is a great way to direct a request to it’s appropiate response. Let’s say you have a page that is meant to list information. With ContextSwitch you can make the action output a regular page for it’s regular call, but, if the user switched the request to ?request=xml. The user will now get the xml version! You only need to create a view script like this: myaction.xml.phtml, and handle the display there.
AjaxContext it’s essentially a wrapper to ContextSwitch that it’s only triggered if an XmlHttpRequest is sent in the headers. I suggest you head over to the ZendFramework’s Official documentation to read in depth on how ContextSwitch & AjaxContext work since there is a a lot to cover, and there is really no point in repeating was is already in the FrameWork docs.

I hope this post helps you, and if you have any more interesting or helpful Zend Framework tips, you are invited to post them in the comments.

8 Comments to “5 Things: Working with Zend Framework and Ajax”

  • That’s nice, but I tried to make this work with Scriptaculous. But I can’t get the auto completion from the server.
    So I have:

    public function acgetstudentbynameAction()
    {
    $test = array('Aaftink','Aagten', 'Aalbers', 'Aalberts', 'Aalbrecht');
    $response = $this->_helper->autoCompleteScriptaculous->prepareAutoCompletion($test);
    }

    But I get no response (empty) from the server.
    What is going wrong? Can you help?

  • OK, solved.
    If I replace ‘$response =’ by ‘echo’ everything goes fine. But I think it’s not nice to have an echo statement in my asyncController.

  • Johan,
    Great to hear that you solved it.. echoing out in your controller should be fine. It can be considered a response. If i’m not using $this->_helper->json() to return a response, I will always echo it out and exit, to avoid the script from returning the corresponding view.
    You can also use die($response);

  • […] test an action in a controller that checks the request to see if is an XML HTTP Request using the isXmlHttpRequest() method provided by the Zend Framework, then you will definitely run into a […]

  • nice work

  • Link exchange iѕ nothing ᥱlse however it is only
    placing the other person’s website link оn yօur page
    at suitable ρlace and otheг person wіll aⅼsо do same foг you.

    Review mү web-site – nagaland state lottery kuil falcon,
    Stefanie,

  • If you desire to increase your know-how only keep visiting this web page and
    be updated with the latest information posted here.

Leave a comment