Introduction To Zend Framework

Once you are online, it’s easy to find new technologies out there. Read some IT articles from websites and you probably think “yeah… it’s cool”, but it’s not really easy to get directly into those technologies, especially if you are a developer.How many months or years you have spent learning some programming languages/paradigms, only to face new things that say “leave it and try this one”?

I believe there are so many PHP developers in web development area, and they are mostly still using pure procedural paradigm. I can imagine sooner or later – just like me – they will read about Zend Framework one day, but instead of thinking this is a good news, maybe they will start worry about their time dealing with learning curve, adaptation, practice, and so on… while on the other hand, Zend Framework promises a rapid development (and yes, it’s TRUE).

This tutorial is for you, who needs quick (but not dirty) approach to get into MVC paradigm brought by Zend Framework.

First I will introduce you briefly what is Zend Framework, and then I will show two main concepts of Zend Framework: use-at-will architecture and MVC pattern.

Zend Framework

I will give you a quick understanding here, Zend Framework (or ZF for short) is a collection of object oriented components/objects that you can choose any and plug into your codes, all in PHP language.

So, it fits perfectly for PHP developers, you don’t need to learn new programming language, also you don’t need to forget your PHP skill and codes. Even better, you will optimize your codes and improve the way you code, using ZF.

In this following tutorial, I assume you already have ZF installed on your machine. If you haven’t, download ZF via and follow the installation instructions.

Use-at-will Architecture

You can create professional web applications that have modern features such Ajax, RSS/Atom feed, web services and so on, all using ZF. But none of those features is the most important issue in ZF, if I want to use ZF technology and its working environment for long time; I don’t think those features are enough since I can do the same things with many different ways without ZF.

Fortunately, ZF has two main and powerful concepts: use-at-will architecture and MVC pattern. Keep in mind these concepts and you can use ZF in the best way that suitable for your condition.

Now, we will discuss use-at-will architecture first.

Imagine you have library of functions that you can choose any of them individually depends on what you need in your application. This is what ZF provides to you with use-at-will architecture.

For example, take a look at this familiar PHP codes:

$sql = “INSERT INTO ms_product (product_code,product_name,description) VALUES (‘01’,’Keyboard’,’Cordless’);

As PHP developer, you may have ton of INSERT INTO queries like this (and much longer than this). Let’s assume several hours after you wrote the query, you have to add price field in ms_product table, then you must change your query to:

$sql = “INSERT INTO ms_product (product_code,product_name,description, price) VALUES (‘01’,’Keyboard’,’Cordless’,100);

Note the changes in bold font, this is the way you usually change your codes with traditional PHP. Of course above is simple query example, but more complex the query, more difficult for you to maintain, even if your query is formatted and wrapped into lines.

Now ZF is coming and you have an alternative to use a ZF component called ZF_DB. You will change your codes as follows:

See the difference? Now your query is more structured, formatted and easy to maintain. In case you don’t have enough time to re-write all queries in this way, you can keep some of your queries unchanging.

That’s how use-at-will architecture works; use ZF in any part you want without you have to change everything.

Below is a more complete showcase how to create form validation with ZF technique.

Look at this example form:

This is a common registration form in HTML that has input fields as follows:

  • username


  • password


  • confirm password


  • e-mail address

As PHP developer, your job is prevent invalid entries from users, assume you have to provide these validation rules in your PHP codes:

  • Username cannot be empty, only contains alphabet and numeric characters, white space is not allowed.


  • Password and confirm password has same validation rules as username, and both should be exactly the same.


  • Reject invalid e-mail (e.g. should be has domain name, @ character, etc)

I think no need to write validation example using traditional PHP here, but I believe you can predict the complexity and how long to build those things, maybe it takes up to 30 minutes, and the original codes will bigger twice or more, just try it if you want.

Now let’s see the following codes and see how ZF handles above validation problems:

I will not try to assure you this codes is written in less than 30 minutes, but we can see it’s short, easy to understand, and easy to maintain and reuse. It’s using Zend_Filter_Input component, where you don’t have to do some complex regular expressions and logic programming, but you have to know how to use this component.

As you can see, $validators is a validator rules that contain array to prevent invalid entries for each fields, then we retrieve input data with this statement:

$input = new Zend_Filter_Input($filters, $validators, $data);

Then all entries will be processed. You can try input invalid data (e.g. username contains white space, invalid e-mail address), browser should return invalid information as the entries don’t match the rules.

In two examples above (Zend_DB and Zend_Filter_Input), you can see the similarity, how the codes is structured well and using object oriented approaches.

Remember to include Zend reference files with statement require_once. In some cases, it’s not so hard to code with use-at-will architecture way, because ZF has very good documentation and examples for each component. But you may find some difficulties in exploring its functions to fit some cases; this is a learning process you must pass through.

But don’t worry if you cannot find ZF solution, you still can combine your ZF codes with traditional PHP or even other framework/library while you are keep on searching the ZF solution. For my personal taste, this flexibility is what I like the most from use-at-will architecture.

For example, mostly of ZF validation codes as explained above is a copy paste from ZF documentation, but when it comes to how to reject password field and confirm password field when they are not equal, you won’t find copy paste solution in ZF_Filter_Input component documentation, except a clue about StringEquals hypothetical class (you won’t find ready to use StringEquals class, you have to create it first).

I also used my own PHP function to that case as alternative way before I finally found how to use Zend_Validate_InArray class later. As I said before, you can use ZF in the best way that suitable for you.

MVC Pattern

I think MVC pattern is a further step after you familiar enough using ZF components with use-at-will architecture way, because MVC pattern is likely to use in relative bigger web application. I bet you won’t take the risk using ZF in a big scale web application without have enough understanding and practices on it.

MVC stands for Model-View-Controller, a paradigm in software engineering that isolates business logic from user interface consideration.

First, we will look at the theory of MVC. This is simple definition of Model, View, and Controller:

1. Model.

The domain logic adds meaning to raw data, e.g. validate e-mail address, calculate taxes.

2. View.

The user interface elements, view is HTML pages in web development.

3. Controller.

Processes and responds to events or user actions, may invoke changes on the model.


On ZF documentation, you will find Zend_Controller located somewhere in the middle of index page, because the documentation is sorted by alphabetical.

Hint: do not read the documentation just like you’re reading Harry Potter! Zend_Controller is where you should read first in order to learn about MVC pattern.

As your application should isolate business logic from user interface, it becomes common using layout or template. Some developers maybe not familiar yet with this, since they are handling all processes starting from database design, web design, web programming – all in the same layer of work – and finally launch it to Internet.

But you should know your work is not finished yet, if you believe in software development life cycle; this is just a beginning of your website life cycle. Sometimes modifications take longer time than initial development, especially if you don’t have robust structure and don’t know where you’re going to.

Some major modifications for example are: changes in layout and design, database structure, additional features, and so on.

So you should consider a structured and robust file system layout in big scale web application, I will introduce you to a typical directory layout as follows:

You may find many versions of layout from other references, but it doesn’t matter if you already know what the point is. This is not fixed format as you can create your own layout.

You can create your layout manually or using wizard template. I am using Zend Studio for Eclipse to generate above template layout. Let’s continue from this layout example.

See the structure and files, note that phtml files that locate below view folder are web pages files that contain PHP codes.

If you create this layout manually using Windows environment, maybe you find problem in creating file .htaccess and complaint to Bill Gates why you just cannot put period character as first character in filename. Don’t worry; you can create a blank document in notepad and save it as .htaccess with File Type: All Files.

Add these codes to .htaccess:

This means all non-resource requests (images, stylesheets) will route to the front controller.

Next, edit html/index.php as follows:

This is called bootstrap file, where all requests are routed through. ZF also recommends you to leave your PHP codes open, without ?> closing tag as best practices to prevent unintentional output of white spaces.

You can set throwExceptions parameter value to true if you want to display more detail error messages, otherwise it will display your customable error messages.

Edit IndexController.php as follows:

This is default action controller.

Next, edit ErrorController.php and enter the following:

Edit index.phtml as follows:

Finally, edit error.phtml as follows:

This is what will be displayed if any error occurred.

Now try to view your site, for example http://localhost/zf/html (assuming your root web folder is zf), if you encounter some error like server error, probably you need to modify file httpd.conf, and find these lines:

Add or delete character # in front of each line as display above, restart your apache and try again.

Don’t expect sophisticate web pages, because all of your efforts only will display “Hello World!” in your browser, but this is where you begin to create your cool web application with MVC paradigm. Feel free to change error message, greeting message, etc.

Maybe you still think it’s too complicate steps only to display “Hello World!”, well, that’s not true if you see the bigger picture. For example, try to put wrong address (e.g. http://localhost/zf/html/anything), and your framework will automatically handle it and return your own error message.

I also believe now you can combine validation codes as explained before with MVC pattern. I hope with these steps, now you have good understanding both in use-at-will architecture and MVC pattern.

Still there are a lot of things to discover, keep practice and learn!

Joko Nurjadi – 20 May 2008

(Visited 92 times, 1 visits today)

12 thoughts on “Introduction To Zend Framework

  1. Thanks for sharing the link, but unfortunately it seems to be down… Does anybody have a mirror or another source? Please reply to my message if you do!

    I would appreciate if someone here at could post it.


  2. I have read a only one of the articles on your website now, and I really like your fashionableness of blogging. I added it to my favorites trap age file and resolve be checking back soon. Cheer repress into public notice my site as ok and leave to me conscious what you think. Thanks.

  3. I've always liked things like sand clocks, lava lamps, and the like to kind of rightful dissipate time staring at it as a cut of catharsis. In a route, it helps me with meditation, to mitigate emphasis and well-grounded assume just about nothing. That's why since I was a kid, instead of dolls and cars I've in any case at ease more of such pieces like sand clocks, lava lamps, harmonious boxes etc. So I was most charmed when I start the[url=] 2-Laser + LED Spry Manifest Laser Ace Gyroscope with Music Effects[/url] from protection the Toys section. It's like a melodic caddy, a spinning top, and a radiance verify all rolled into one. Which is prodigious entertainment! The gyroscope wishes spin for about a minute. The laser light manifest with accompanying music makes this gyroscope a very incomparable fiddle with that my friends have also been most amused with.

    My dogs are also pretty eccentric about the laser gyroscope I got from They evermore attend the gyroscope as it spins, although at earliest they kept barking at the laser light advertise, and also because it produces music. But after they got worn it, they've stopped barking but simply save up following the gyroscope whenever I start spinning it. Kids are also unbelievably amused next to it. On occasion it's moral to take pleasing toys about the lineage so that you can pull out the diminutive on while the kids are being amused or playing with it while you live prepare scoff or receive changed. The gyroscope is one such trinket with this purpose.

    The gyroscope I bought from has a dragon as a map on it, and produces a gegenschein indicate with red, obscene, and green colours. Pit oneself against a look at the pictures I've uploaded of the gyroscope with laser luminosity show. The music produced from the gyroscope is not that enormous but facts sufficiency to consider any redone customer to the house. The gyroscope is red and raven, making it look extraordinarily cold, and measure virile with that dragon imprint.

    The music dismount attack make clear gyroscope runs on 6 LR44 batteries, which are replaceable anyway. I've also utilized this gyroscope to surprise my girlfriend during our anniversary celebration. I did the cheesy thing of decorating the hotel elbow-room with roses and when I led her in, I started up the gyroscope as well so that the laser brighten clarify produces a dreamed-up effect. I also had some battery operated candles so all the understanding effects created a rather dreamt-up atmosphere. She loved it, by the way, to my relief. I also bought the candles from These days it seems to be my oversight shopping placement for all gifts and ideas in compensation romanticist occasions.

    Since Christmas is coming, this laser radiance make clear gyroscope can dialect mayhap be a superb Christmas favour in behalf of the toddler or in spite of the humour! Alternatively, the gyroscope can altogether be a kindly totalling to the ordinary Christmas decorations. I can take it as given placing it within a mile of the Christmas tree and maybe spinning it when guests succeed in the house. Looks like [url=][/url] is getting my subject yet again!

  4. A shared spider's web hosting military talents or effective hosting utilization or receive assemblage refers to a web hosting checking where diverse websites reside on harmonious net server connected to the Internet. Each locale "sits" on its own partition, or section/place on the server, to regard it separate from other sites. This is generally the most thrifty privilege owing hosting, as uncountable people allocation the entire bring in of server maintenance.

  5. [url=]viagra[/url], hinder my site. If you are interested in escort, be aware [url=]huren[/url] or [url=]ogloszenia towarzyskie[/url]

Leave a Reply