Playing with AjaxContext

I had a play with the AjaxContext code that Matthew posted to the laboratory and it's quite nice.

This is how I set it up.

Firstly I grabbed the files from the laboratory subversion repository and placed them in my lib/ directory. I then took updated my path using set_include_path() ensuring that the new folder was first in the list.

Having set up the environment, I then loaded the action helper in my bootstrap:

PHP:
  1. $ajaxContext = new Zend_Controller_Action_Helper_AjaxContext();
  2. Zend_Controller_Action_HelperBroker::addHelper($ajaxContext);


Which is nice and easy!

We are now able to make an action ajaxable. I pulled out the feedback action in the review controller within the Places example application. This action is already "ajaxed" and so the view scripts create an XmlHttpRequest to the action and know how to respond when Json is returned. Ideal for testing the context helper.

In the ReviewController, you need to add a new public member variable called $ajaxable:

PHP:
  1. class ReviewController extends Zend_Controller_Action
  2. {
  3.     public $ajaxable = array('feedback'=>array('json'));
  4.    
  5.     // continues


The array contains a list of actions within this controller than may receive Ajax requests and a list of acceptable formats. In our case, we only support json, but you could equally support xml, html csv or plain text depending on the use of the action.

We then need to initialise the context in preDispatch():

PHP:
  1. public function preDispatch()
  2. {
  3.     $this->_request->setParam('format', 'json');
  4.     $this->_helper->ajaxContext()->initContext();
  5. }


It is intended that you can pass through which context you want using a request variable called 'format'. Unfortunately, you can't not have it in the request at the moment. This bug will be fixed :)

initContext() is actually quite a simple function. It does lots of checking to ensure that the context needs to be changed and then it changed the suffix for the view scripts to {format}.phtml, sets a content type header in the response and finally disables Zend_Layout's layout system (if it's in use).

In this case, the view script that is now rendered is called feedback.json.phtml which contains the following:

PHP:
  1. <?php
  2. echo Zend_Json::encode(get_object_vars($this));


This simply encodes all the variables that have been assigned to the view into Json format.

The processing of the request continues as normal and a little later, at the end of the dispatch cycle, the response is echoed back the browser with a nicely formatted Json object as intended.

Posted by Rob on 7th January 2008 under The Book |

6 Responses to “Playing with AjaxContext”

  1. Matthew Weier O'Phinney responded on 08 Jan 2008 at 2:03 am #

    One note — you don’t have to setup the helper in your bootstrap; helpers such as this one can be loaded on-demand in your controllers simply by invoking them: $this->_helper->ajaxContext->initContext().

  2. Fercho responded on 09 Jan 2008 at 7:32 pm #

    Can you put the code to download ? It could be nice so we can learn more about this interested helper.

  3. Sven responded on 13 Jan 2008 at 9:00 pm #

    A couple of questions:

    • If “This action is already “ajaxed”" then why do you need to use this ajax helper?
    • In the array you set up for $ajaxable why does feedback = &gt?
  4. Rob responded on 13 Jan 2008 at 9:13 pm #

    Hi Sven,

    The helper is intended to ensure that you can send back the correct format to the browser.

    For the second point: I didn’t handle html escaping properly here in the post whilst implementing the syntax highlighting. Should be fixed now.

    Regards,

    Rob..

  5. Umpirsky responded on 03 May 2008 at 11:49 am #

    I’ve done it like this, but 3 problems occured:

    1. My view script (convert.json.phtml in my case) is not rendered, even if I have:

    class AjaxController extends Zend_Controller_Action { public $ajaxable = array(’convert’=>array(’json’));

    public function preDispatch()
    {
        $this->_request->setParam('format', 'json');
        $this->_helper->ajaxContext()->initContext();
    }
    

    }

    1. I cannot acces post data in my controller with:

    $this->getRequest(’text’);

    even If $_POST['text'] is set and I can use it.

    1. When I echo something in my ajax controller action, [] is appended to start of my response to the client. Headers are properly set to json.
  6. Umpirsky responded on 03 May 2008 at 11:51 am #

    I didn’t mentioned that my ajaxable attribute is

    public $ajaxable = array(’convert’=>array(’json’));

Comments RSS

Leave a Reply