A Zend Framework tutorial, part one

by Naneau

Some of the code in this tutorial (all four parts) does not work with version 1 of the framework. Especially the part on rendering views has changed.

After reading many tutorials, I decided it was time to write one myself. Not because the ones out there are bad, most of them are really good reads, and will get you started with the Zend Framework in a jiffy. But in my opinion they take a wrong approach towards getting people started in application development.

In many of these tutorials you’ll find sentences like “we’re going to make a simple weblog,” and they immediately assume it’s clear what that will encompass. They start with code examples, controllers and views, but never with what in my opinion is the base of a MVC application: the models.

I will use a simplified version of the design approach I described before. I will assume you speak PHP, and have the framework running on a server, see my tutorial on setting up the framework if you don’t. The application we’re going to develop will be a very simple weblog. That being said, I doubt I will write the complete code for it. The focus will be on requirements engineering and application design, not coding.

So, let’s get started. You want a weblog, and the current software out there doesn’t fulfill your needs. Now, take some time to think about what kind of things are needed for a weblog. No really, take some time. Sit down, get a piece of paper and write down what comes to you mind. Be messy, use exclamation marks, underline things, put circles around other things. Think about what you want, not what you know you can do, or what others have done. In short, write down the requirements for your application.

If you’re doing this for somebody other than yourself, you may want to fire up your word processor and create a formal report on the requirements. If, like me, you’re just working on a little project for fun, just pin the piece(s) of paper to your wall. At any rate, you want to end up with a set of requirements that you can refer to and work from. You want to know what your application should do, you do not want to know what it will look like (at least, for now).

Now, you may be wondering when the Zend Framework comes into play. Well, soon! There’s one last step to do before you can start thinking solutions. You have to create some kind of diagram from your requirements. There are a lot of modeling languages and techniques out there. There’s UML, ORM, ER, and they’re all good. If you’re not comfortable with any of them just make one up, but be consistent.

You’re going to have to find the entities that exist in your application. Put simply, an entity is something that is. Entities are nouns. For our blog application a post (or entry) is an entity, reactions to posts are entities as well. No matter what modeling language you use, you will always be able to see the entities somewhere in the diagram. Apart from entities, there are relations. Relations are verbs. Entities relate to other entities. Users create posts. Posts have reactions.

The model

For our little weblog I have created the following diagram. As you can see, I just wrote it down on a piece of paper. I didn’t use any “real” modeling language, to keep things simple. This is the most basic form of a diagram. There are no constraints, the relations don’t even have names. You will also notice that my handwriting isn’t that readable, I am sorry for that. There are 3 entities, users, posts and reactions. Depending on your own requirements, you may have other entities.

Enter Zend Framework. Yes, it’s finally time to start coding. Once you have found out what entities we’re working on we can fire up our editor. We have found the Models for our MVC application, we are going to create classes for all of them like so:

1
2
class Post extends Zend_Db_Table {
}

As you can see, I make my models extend Zend_Db_Table. If you don’t know what Zend_Db_Table can do for you, I would strongly suggest you take a look at the manual, because I’m too lazy to explain it in detail. Also, be sure to have a look at table relationships. You should know that Zend_Db_Table works with tables in a database. So we’re going to have to create those first. I have created a sample SQL file you can use (it is mysql specific though). It contains some attributes (think fields) I think our three models should have. Of course, with your own requirements for a weblog, these may differ.

If you have a standard layout for your application, you will have a separate directory for your models somewhere. Create three files there, one for each model, User.php, Post.php and Reaction.php.

This is what my User.php looks like:

1
2
3
4
5
6
<?php
require_once 'Zend/Db/Table.php';

class User extends Zend_Db_Table {
     protected $_dependentTables = array('Post', 'Reaction');
}

My Post.php looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php

require_once 'Zend/Db/Table.php';

class Post extends Zend_Db_Table {

    protected $_referenceMap    = array(
    'User' => array(
    'columns'           => array('user_id'),
    'refTableClass'     => 'User',
    'refColumns'        => array('id')
    ));
    //users 'own' posts
}

And finally my Reaction.php looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php

require_once 'Zend/Db/Table.php';

class Reaction extends Zend_Db_Table {
    protected $_referenceMap    = array(
    'User' => array(
    'columns'           => array('user_id'),
    'refTableClass'     => 'User',
    'refColumns'        => array('id')
    ),
    'Post' => array(
    'columns'           => array('post_id'),
    'refTableClass'     => 'Post',
    'refColumns'        => array('id')
    )
    );
    //users place reactions
    //reactions belong to a post
}

These three short files will form the basis of our application, believe it or not. You may wonder why I have put so much time into creating a few lines of code. If you do, I don’t think you got the point of this tutorial. In that case you will like the second part better, where I will go into creating controllers.