Zend View Helpers with AJAX
March 4th, 2009This is the second part of last weeks tutorial. Here we add some JQuery code and look at how Zend_View_Helper_* classes can be written in such a way that they behave the same when they’re invoked on the server and called asynchronously via an AJAX request.
Feel free to follow along by downloading the project code.

Hey this is great. Any plans to do some casts using the built in ZendX_JQuery library that’s in Zend Framework 1.7.5?
Hi Adam,
I’d love to, however last I checked it’s still in the incubator. Is that still the case?
Jon
I love these tutorials. they’ve been very helpful along with a variety of other learning materials I have been using.
This last tutorial for view helpers threw me off for quite a while. I am not sure if I have a bug, or there is a bug in the Zend framework for view helpers.
I decided to use /application/views/helpers as the location for my helpers.
So, I have
$view->addHelperPath(APPLICATION_PATH . Globals::getConfig()->view->helper->path, Globals::getConfig()->view->helper->prefix);
the config for path and prefix are
path – /views/helpers
prefix – View_Helper
With that setup, it doesn’t work.
So, I spent most of the day playing around with it, trying everything from adding the views/helpers to my include, adding /application to my include (which it was already there) to lots of other stuff.
Finally, I decided based on an error message about not being able to instantiate the view helper the second time around, I changed the setup in my config to be
path – /views/helpers
prefix – Views_Helpers
All works just as your tutorial explains. Note: all I changed was the prefix of the help was the same as the underlying directory it was in.
Are you aware of this issue?
Hi Jim,
Thanks for the kind words.
in the video before this one, I mention briefly that its important to namespace your helpers. The ZF architects made a conscious decision to start all their classes with Zend_* in an attempt to keep a clean namespace (since PHP doesn’t support proper namespaces). That being said, its important to use a prefix of your own. If you pick View_Helper_* as the prefix for your view helpers, then all your View Helpers would be View_Helper_View_Helper_* if I’m not mistaken… I would suggest a prefix like App_ or My_ instead since you’ll invariably start to build up a collection of your own ZF components that will be re-usable from project to project.
Hope that helps!
Oh, maybe I am confused. In the release notes for the 1.7.5 framework is lists this:
* ZendX_JQuery in extras library (see extras folder in the full package)
I took that to mean it was part of the release, but maybe because it’s not in the main Library it’s not yet fully baked? I am still new to Zend Framework. That may also explain why I am struggling to get it implemented and working.
Hey ! that’s a great tutorial !
Thanks to you I’ve been able to use ajax and JQuery in my zend project.
I thought it were harder !
Hey Jon,
I just came across a problem in IE6 that by returning false it caused IE6 to lock the cursor in whatever input box it was on. Here’s what i have:
$(“#form-user-name”).change(function()
{
doSomething();
// returning false locks cursor on input
// box in IE6
return false;
}
I was just wondering why you want the javascript to return false by default.
Hey Jon,
Love the videos and the site in general. I tried to copy this project, but keep getting this error:
call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, ‘__PHP_Incomplete_Class::tabbedbox’ was given in C:\xampp\htdocs\BC\library\Zend\View\Abstract.php on line 341
My Zend view helper is called Tabbedbox in App/View/Helper. It works if I do $this->view->tabbedbox(), but not if I do $this->session->view->tabbedbox(). Do you know what could cause that? Did I miss some sort of session code in my bootstrap or index?
Hi Jon. Very clear and nice tutorials.
P.s. I thinks you have a bug in link to code :
current link goes to http://www.zendcasts.com/zend-view-helpers-with-ajax/2009/03/zendcasts-ep12-13/,
but real code is in http://www.zendcasts.com/wp-content/uploads/2009/03/zendcasts-ep12-13.zip.
Thanks Serge! I’ve updated the link.
Hi,
Nice post.Easy to understand.I am new to zend framework.
Can you do a screencast for an ajax based login form along with validations?
Hey zse, why would you ajaxify a login form? usually login forms move you into a different part of your application…
Jon, awesome site… About passing objects through from controller function to the view helper: it is easy for strings but what about more complex objects? For example, using Zend_Service_Flickr_Image, something like the $photos object from this:
public function galleryAction()
{
try{
// create new flickr object
$flickr = new Zend_Service_Flickr( ‘MY_API_KEY’ );
//get the photos by the user.
$photos = $flickr->userSearch(‘ userid @yahoo . com ‘);
$this->view->photos = $photos;
}catch(Exception $e){ throw $e; }
}
How can one pass $photos through to the Helper and get the helper to work on it for us, then pass it back to the asyncController (or to the index view for that matter)?
Thanks!!!!!!!!!
0 in Ruby – true
Hi, Jon !
Thanks a lot for the job you are doing for us. Your video podcasts inspires to create clean, accurate and simple to understand code. You are doing the world better and this is very important. We love you ! )