Zend Framework Starters
Here is a collection of the better Zend tutorials, resources and tidbits I have started collecting.
First off, here is probably the best quick tutorial to the ZF, or rather its the best that I have seen so far.
Something obvious, that cost me time, was that when you create a custom class in Zend for which to extend other classes such as controllers from, you must place the new custom class in your applications include path, e.g create a new directory in your applications dir called ‘custom’, and then add this to your damn include path
set_include_path(‘../library’ . PATH_SEPARATOR .
‘../application/custom’ . PATH_SEPARATOR . get_include_path());
Also important, in any child classes of the new custom class:
If we want an init()function in any of the child classes, we must also call parent::init() from that class.
Two decent Apress books on the Zend Framework, http://www.amazon.com/Pro-PHP-Patterns-Frameworks-Testing/dp/1590598199 and Practical Web 2.0 Applications with PHP. The latter is especially recommended…
The symlink for library directory on A2 is library -> /usr/lib/php/ZendFramework/latest/library/. Fascinating, I know. To remove a symlink, do unlink symbolic_link.
Zend Forms and Related
Fetching row data from mysql in Zend Framework.
From the official Zend docs, here’s the basics of a form controller – nice.
class UserController extends Zend_Controller_Action
{
public function getForm()
{
// create form as above
return $form;
}
public function indexAction()
{
// render user/form.phtml
$this->view->form = $this->getForm();
$this->render('form');
}
public function loginAction()
{
if (!$this->getRequest()->isPost()) {
return $this->_forward('index');
}
$form = $this->getForm();
if (!$form->isValid($_POST)) {
// Failed validation; redisplay form
$this->view->form = $form;
return $this->render('form');
}
$values = $form->getValues();
// now try and authenticate....
}
}
And $values = $form->getValues(); returns an assoc array of values, like so:
Array ( [username] => pokero [password] => 99998883 [login] => Login )
To start off your form, probably best to do the following – set the form action and method:
$form->setAction('/resource/process')
->setMethod('post');
Also, to render a form in a view, just stick this in your view:
<?php echo $this->form ?>
And here is a list of the validators shipped with the Zend framework (frustratingly hard to find on the Zend site
), which I found on this great ZF blog:
- Alnum
- Alpha
- Barcode
- Between
- Ccnum
- Date
- Digits
- EmailAddress
- Float
- GreaterThan
- Hex
- Hostname
- InArray
- Int
- Ip
- LessThan
- NotEmpty
- Regex
- StringLength
OK, I eventually found them on the Zend site, here.
And a list of the different form elements:
- Zend_Form_Element_Button
- Zend_Form_Element_Captcha
- Zend_Form_Element_Checkbox
- Zend_Form_Element_File
- Zend_Form_Element_Hidden
- Zend_Form_Element_Hash
- Zend_Form_Element_Image
- Zend_Form_Element_MultiCheckbox
- Zend_Form_Element_Multiselect
- Zend_Form_Element_Password
- Zend_Form_Element_Radio
- Zend_Form_Element_Reset
- Zend_Form_Element_Select
- Zend_Form_Element_Submit
- Zend_Form_Element_Text
- Zend_Form_Element_Textarea
Form styling and decorators
Will be adding more on decorators here, but for now, just a quick one to sort the annoying dt and dd elements that render the labels on different lines in default forms. Just sticking a <br> tag after each <dd> will sort that:
<script type=”text/javascript”>
$(document).ready(function(){
$(“dd”).after( $(“br”) );
});
</script>
And something that really drove me mad for while trying to get the info, is the really simple way of rendering a different than standard view for an action:
$this->_helper->viewRenderer(‘popular’);
This was well hidden in the docs on Zend site, they really try and make shit difficult to find, f*ckers. The thing is most of the examples on the Zend site actually assume you are instantiating a View object from scratch – of course it is explained here that in your actions the view object already exists, and there is a much simpler way.
For setting up an automatic email verification system, this example from Cake PHP is great. And a very nicely written piece of code on validating identical fields. Good one on throwing exceptions.
For an example on a basic Zend Form in action, check out this Lotto Results checker form.
Something else you should really do is change the default htaccess from, for example,
RewriteEngine on
RewriteRule !\.(swf|js|ico|gif|jpg|png|css)$ index.php
to the following, as suggested here. This has the major advantage that you can now add non-framework files easily to your public folder.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php
Useful redirect is return $this->_forward(‘index’);
And styling Zend forms with Dojo…simple example and worth a look.
And on that link above, here is some nice simple styling, easily expandable for Zend Forms:
/* Zend Form Styling */
.zend_form, .zend_form_dojo {
width: 700px;
margin: 5px auto;
padding: 2px;
overflow: auto;
}
.zend_form dt, .zend_form_dojo dt {
padding: 0;
clear: both;
width: 15%;
float: left;
text-align: left;
margin: 5px 5px 5px 0;
}
.zend_form dd, .zend_form_dojo dd {
padding: 0;
float: left;
width: 68%;
margin: 5px 2px 5px 0;
}
.zend_form p, .zend_form_dojo p {
padding: 0;
margin: 0;
}
.zend_form input, .zend_form_dojo input, .zend_form textarea, .zend_form_dojo textarea {
margin: 0 0 2px 0;
padding: 0;
}
//

Newbie question:
Why use Zend Forms at all? I put a lot of time and effort into creating html docs with jquery, and I am quite satisfied with the forms that I have produced in the html, so why do I need to generate forms on the fly? Obviously I am missing something, but I can’t put my finger on the use cases for Zend Forms.
Thanks for any thoughts you may have.
- Michael
Hi Michael,
thanks for posting! I would say the question is really “why use the Zend Framework at all?”, as Zend Form is only a constituent part of the framework. Main reasons for using ZF and therefore ZF forms would be that it is an object oriented MVC framework, and all the advantages that being so brings – code re-use, scalability etc…
I mean you can have jQuery or Dojo forms in the Zend Framework of course, and if you had a nice little class for your jQuery form, and were using the ZF, you wouldn’t necessarily need to “put a lot of time and effort” in every time you wanted to have a form on a html page.
I’m new to this too, but it makes sense I think.
Cheers
[...] have another post with info on Zend validators, but here’s a very handy class to specify a maximum and minimum [...]
Good effort. Keep it up