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.

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 |
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.
Comments
Thanks
cool. your articles will pull much more sound in a time
excellent. thanks
Noticed that your link to “my tutorial on setting up the framework” goes to your base blog link, not here: http://naneau.nl/2007/05/08/setting-up-the-zend-framework/ as it should. Thanks for the help with ZF!
thank you, fixed it
Finally, I’ve been searching for hours to find something about the Model in ZF. This should be enough for me to find my way.
well i have started with a sample model , but i was not sure about my model concept. thats why i started searching for any model in Zend Framework . After spending some hours in googling model implementation , i have found this one.
Simply great.
I just had started to learn zend framework and searching some useful stuff for development and i found this very helpful to me. And hopefully i can do a web development using zend. Thanks!
your article is very helpful…. I never though that model concept can be that simple understand… Thank you…
[...] Naneau » A Zend Framework tutorial, part one – good head-start too [...]
Thank You Very Much For Showing This InfoCan You Look At THis?,
Ive Been Looking At THis Site,
Thank you… very much my frind
This was a well written tutorial and it was very helpful too. Thank you very much.
Good luck with the site I want make the best use of my incredible default Wanna good joke? What is the difference between a fisherman and a lazy schoolboy? One baits his hook, while the other hates his book.
Can someone clear up my confusion about DRI and ZF. Looking at your sample DB schema, it appears that you are relying on the DB for declarative referential integrity (using CONSTRAINT, REFERENCES, CASCADE clauses with InnoDB). Looking at the ZF manual, it says:
Skip declaration of $_dependentTables if you use referential integrity constraints in the RDBMS server to implement cascading operations.
I notice you are declaring $_dependentTables for User. Is that necessary/correct?
Also, I notice you don’t declare $_dependentTables for Posts, for which Reaction is a dependent table, right?
Can someone clarify?
Thanks!
[...] Một loạt bài tutorial theo mình là khá dễ hiểu: http://naneau.nl/2007/04/21/a-zend-framework-tutorial-part-one/ Bài viết này giúp bạn tạo custom router (liên quan đến ứng SEO vào trang web của [...]