Archive for January, 2008

Extending Controllers

Spotsec has posted about Extending Zend_Controller_Action and keeping init() and everyone should read it!

Essentially he is saying that when you extend Zend_Controller_Action, you should put your initialisation code into the constructor rather than init(). The tricky bit is that Zend_Controller_Action’s constructor takes two parameters which people seem to forget about. If you don’t include them, then everything breaks.

We do this at work (and we use control+click to find the function signature, not google!)

Posted by Rob on 13th January 2008 under Around the web | No Comments »

Plans for ZF 1.5

Darby has posted to the Zend Framework mailing list that plans are now in place for the first preview release of the next significant version of Zend Framework, version 1.5.

The code freeze will be Tuesday 22nd January at 9pm (PST), from which point changes to Zend Framework code will be carefully managed to focus on the 1.5 release.

In a followup post, Matthew rather helpfully posted a list of key new or improved components that will be included in 1.5. It was off the top of his head, so may miss some stuff out:

Okay, off the top of my head, new and/or improved components include: * Zend_Auth_Adapter_Ldap * Zend_Build/Zend_Console * Zend_Controller additional action helpers * ContextSwitch and AjaxContext * Json * AutoComplete * Zend_Form * Zend_InfoCard * Zend_Layout * Zend_OpenId * Zend_Search_Lucene improvements and bugfixes * Zend_View enhancements: * actions * partials * placeholders * New Zend_Service consumables (not sure which ones will be final) I’m sure there’s more, but, as I said, that’s off the top of my head. A good place to look is on the wiki proposals page (look at what’s currently in the incubator, and see what core proposals may be present that you haven’t seen before), and also in the issue tracker.

Wil also mentioned the All 1.5 Issues filter on the issue tracker which provides details about all the little changes that are going into this release.

Posted by Rob on 10th January 2008 under News | 2 Comments »

Zend Form moved to incubator

In preparation for the 1.5 preview release, Zend Form has moved to the incubator SVN tree.

This is a really good time to get a copy of the trunk and have a look at Zend_Form to find out how good it is and report any problems you have, especially with the API. Note that it’s still in active development and not completely stable.

Posted by Rob on 10th January 2008 under News | 2 Comments »

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 Comments »

Whitepaper

A few weeks ago, we were asked to write a white paper on what the Zend Framework is for the Marketing people at Manning. Obviously, being a programmer, I reused and re-factored what I had already written for Chapter 1 of the book.

It's interesting taking an excerpt out of a larger body of work and trying to make it stand on its own. I ended up re-wording more than I expected and also re-ordering bits as they didn't make sense order that they are in Chapter 1.

I got some feedback on it a couple of days ago and apparently it looks useful which is good. It's going to be formatted up and presumably it'll be published on the Manning website. if it is, no doubt I'll link to it from the Resources page here.

Of course, now I need to go and rework Chapter 1 with the improvements that I've made!

Posted by Rob on 4th January 2008 under The Book | 3 Comments »

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!

Posted by Rob on 3rd January 2008 under The Book | No Comments »

Zend_Form proposal progress

Following all the excellent comments that have been left, Matthew Weier O'Phinney has posted a comment on the Zend_Form proposal about the focus of the Zend_Form proposal:

1) Based on user requests, I'll be investigating adding this functionality (element grouping) 2) I'm considering allowing each element access to all other values, as this would keep them atomic, but allow you to use other values as part of your validation. 3) empty() will be used for the requiredness check (i.e., !empty() will indicate a value is present) 4) Any ideas for lazy loading I'd gladly accept

I'm especially pleased to see element grouping as we don't do forms without <fieldsets> and so it will make Zend_Form much more useful if it handles grouping of elements.

Allowing access to other values is useful for validating only if another form element is set, so I can see the benefit there. I think it would also be useful for those cases where you need the user to enter their password twice to confirm it.

All in all, it is good to see the proposal process working so well and I'm sure we'll have a much more useful Zend_Form component as a result.

Posted by Rob on 1st January 2008 under News | No Comments »

« Prev