Posted by Andre Liem

* This blog post is part of Invoke’s participation in the 2008 Vancouver Blogathon for Charity

Why use the Zend Framework?

In the past few years, web development has become a very competitive arena for programming languages, frameworks, libraries and IDE’s. As a result, there has been a plethora of options for programmers to choose from. In most cases, a significant amount of time can be spent on searching for tools to speed up development instead of recreating from scratch. This article is aimed at php developers who are looking to learn more about how not to reinvent the wheel, in particular, with the new Zend framework. This is not a comparative list of all php frameworks, as there are many good ones out there, CakePHP and Symphony to name two. I’ve evaluated other frameworks to some degree, not exhaustively by any means, and the Zend framework seems to fit nicely with my requirements. It may not for you, but I’ll try to persuade you in this article :) .

A little about the Zend Framework

The Zend framework is only a few years old and as of this article has reached version 1.6 Release Candidate 1. In some ways, Zend is very similar to the PEAR framework, but not quite as large and for lack of a better word, more “web 2.0â€? focused. So what does that mean? For one, it supports Model View Controller (MVC), caching, form generation, configuration management, JSON parsing, DOJO (AJAX) support, mailing, several web services API integrations, and more. The downside of a new framework is completeness and community. Some of these objects do very little (yet), and your best source of help is yourself or googling old articles about previous versions of Zend.

Aside from this, the Zend framework offers power and flexibility. You can choose what objects you want to use, and it’s very easy to integrate Zend into other frameworks. Given this alone, as a php developer you should learn Zend because it’s just as useful as PEAR. I’ve already noticed some articles popping up about how to use Zend objects within other frameworks like CakePHP. The flexibility of Zend is my favourite feature, as I’ll show later it’s very easy to integrate other tools into the framework.

Coding Faster… Rapid Application Development

So how can you achieve Rapid Application Development (RAD) with Zend? Well, when I use the term RAD, I may not be falling inline with the traditional definition but I’m referring to the idea of coding less. Perhaps I should just say three words, Ruby on Rails. If you don’t know what Ruby on Rails is then read this article

You may ask why not just use Ruby on Rails or CakePHP? My primary reason is again flexibilty, it’s very easy to pick and chose the features you want to use.

Tools you’ll need and what they do

Download and install these tools:

The Development Process Flow

Here are the steps. Make sure you’ve installed all the applications and followed their documentation.

Part A – Generating the database classes without coding.
This part will show you how to generate database objects using db designer.

  1. Design the database in fabFORCE’s visual db designer. Very nice and easy to use.
  2. Save db designs to native fabFORCE XML format.
  3. Save db designs to SQL create script for mysql.
  4. Convert fabFORCE XML (from step 2) to propel’s native XML format. Use this conversion script *
  5. Create the database with the sql script (from step 3).
  6. Run propel on the converted XML from step 4.

*Unfortunately, this script doesn’t fill in the foreign key rows, so you’ll have to manually update the XML outputted from this script. Regardless, this tool is still a major time saver.

At this point, you now have a set of classes that represent your database layer, or your CRUD layer (CREATE, READ, UPDATE, DELETE). Oh, and I forgot, you also have your database setup too.

Part B – Setting up Zend and Eclipse

  1. Create the directories for a Zend MVC framework (learn here). You’ll have two areas, one for the application MVC code and another for the web root.
  2. Startup eclipse with a new workspace.
  3. Create a new project in eclipse that points to the “applicationâ€? directory
    (the one with the MVC folders).
  4. Create a new project in eclipse that points to the “web rootâ€? directory.
    (the one with the index.php and htaccess file)
  5. Create a new project that points to your Zend library folder.
  6. Create a new project that points to your propel folder.
  7. Add project references to the application project from step 3:
    a. Right-click project -> Properties
    b. Select PHP Include Path
    c. Select Projects tab
    d. Click Add
    e. Select Zend and Propel.
  8. Build all of the projects (Project -> Build All)

At this point, your environment is almost setup.

Part C – Programming

  1. Copy the generated propel objects (from part A – step 6) into the “modelsâ€? folder of the MVC.
  2. Build the project again.
  3. Setup the index.php bootstrap file to include appropriate libraries. **
  4. You’re done!

**This part can be a bit tricky so here’s some sample code below that would fit at the top of your index.php bootstrap file.

set_include_path('.' . PATH_SEPARATOR .
'../library/' . PATH_SEPARATOR .
'../application/models/' . PATH_SEPARATOR .
'../application/config/' . PATH_SEPARATOR .

//In the above:
//../application refers to the relative path to your MVC folders.
//../library would contain Zend and Propel

require_once 'propel/Propel.php'; //generated db objects

* include all propel models here
//This is where you include classes generated by propel
require_once 'databasemodel/Person.php';
require_once 'databasemodel/PersonPeer.php';

With this model setup, you can code CRUD actions like this:

Example 1 – Controller – Create a record (in controller)

$person = new Person();

Example 2 – Controller – Read records

$c = new Criteria();
$people = PersonPeer::doSelect($c);
$this->view->users = $users;

Example 2b- View – Echo records

<?php foreach($this->users as $currentUser):
echo $this->currentUser->getUsername();
endforeach; ?>

Example 3 – Controller – Update records

$c = new Criteria();
$currentUserId = $this->_request->getParam(“userIdâ€?);
$c->add(PersonPeer::ID, $currentUserId);
$currentPerson = PersonPeer::doSelectOne($c);

Example 4 – Controller – Delete records

$c = new Criteria();
$currentUserId = $this->_request->getParam(“userIdâ€?);
$c->add(PersonPeer::ID, $currentUserId);

In the examples above, eclipse would provide intellisense auto completion if you pressed ctrl + after typing $person->. Aside from making coding faster, this reduces error because you’re only using methods that actually exist. Eclipse does this by compiling the included libraries when you build the projects.

You may also notice that I’ve shown the basic CRUD (Create, Read, Update, Delete) use cases that Ruby on Rails and CakePHP make so easy. Another framework, Symphony, actually uses propel too so you can often rely on Symphony’s community for help when it comes to making database calls with Propel.

The first steps may seem like a lot to do, but it’s really not that complicated and the time saved in the future is well worth it. You may wonder why I didn’t use Zend’s Zend_DB Object? I really prefer Propel DB because it removes the need to code any SQL and it generates code from XML.

To reiterate the most important part of this article, you have a lot of flexibility with this setup, you can pick and choose what aspects you want to follow. From here you could easily integrate more Zend objects without having to worry about the model layer.
Further thoughts

The one area I did not cover, and which is a major bonus for frameworks like CakePHP and Ruby On Rails, is AJAX support. As of this article, Zend has just shipped with DOJO support and they will continue to provide more in the coming months. There is not enough room to cover DOJO, but my initial experience with DOJO is very positive. All I can add is that it seems like it will be easy to integrate DOJO as a fairly independant AJAX layer that doesn’t become tangled in your PHP code.