First Experience with Zend Framework
Friday, September 3rd, 2010I wrote a review of Zend Enterprise PHP Patterns a while back. While I read the book, I had yet to jump into the Zend framework and really make a project that used it, or any of it for that matter. While doing a project for a client I had need of making several forms. Nothing is more tedious to me than making forms. The html tags for labels, the form input, and then the PHP on top of it to give default values. This is OK when making a short form. But I get extra sick of it when doing a form with even ten or more fields. When faced with such a situation this last week I decided to try just a piece of the Zend Framework.
This is possibly my favorite thing about the framework, the ability to use as much or as little as you would like. It gives great flexibility and allows you to include parts into a currently existing project.
To get started, you need to tell your PHP application where zend is, in addition, starting the autoloader at this time really makes life easy. The following code does just that:
-
require_once ‘<wherever Zend is>/Autoloader.php’;
-
$autoloader = Zend_Loader_Autoloader::getInstance();
With these two items in place you can just begin to call Zend Classes and Functions without another thought about it. I put this in my included header.php file, where I have all kinds of includes. Now my functions are available to me wherever I am.
To start your form it is very easy.
-
$form = new Zend_Form();
That is is, we have started a form. Everything else will use the newly created $form object and go forward from there.
Next, you must add attributes and elements to your form. I found certain parts of this very poorly documented. I wish there was a list of acceptable options, etc for each form addition type but there isn’t. Maybe I should just not complain and write it, but I digress. Adding attributes is rather easy
-
$form->setAction(‘./action.php’)
-
->setMethod(‘post’)
-
->setAttrib(‘id’, ‘form_id’);
Now adding form fields, a couple options here, both quite easy. First is to just use the $form object and add directly:
-
$form->addElement(‘text’, ‘text_input’, array(‘value’ => $_REQUEST[‘text_input’], ‘label’ => ‘Text Input:’));
The arguments passed to the addElement method are type, name, array of options.
The second option is to create a Zend_Form_Element object and add that to the $form object:
-
$select_input = new Zend_Form_Element_Select(‘select_input’);
-
$select_input->setLabel(‘Select Input:’)
-
‘Option0′ => ‘Option0′,
-
‘Option1′ => ‘Option1′,
-
‘Option2′ => ‘Option2′))
-
$form->addElement($select_input);
The last bit, actually rendering your form. Some caveats here, Zend Framework components can be used outside of the Zend MVC environment, but you still must define a view in order for the form to actually come up. If you do not, you will get an exception. It isn’t hard though, the following will do it for you:
-
$form->setView(new Zend_View());
-
echo $form;
Viola! The form magically appears in front of your eyes. A couple things to remember, anything that can be attribute of an HTML form of input elements can be added to the Zend_Form in the options array. This includes, value, class, specifically defined ID, etc. I will be using Zend_Form much more often. It is so easy to get forms together quickly and easily.