The Zend Framework has been unveiled! Although it is still in the early stages of development, this tutorial highlights some of the best of what's available now and guides you through the process of building a simple application.Zend has chosen to release the framework and involve the community early. In the same spirit, this tutorial is written to showcase the framework as it exists today. Because this tutorial is published online, I'll update it as the framework evolves, so that it remains relevant as long as possible.
RequirementsThe Zend Framework requires PHP 5. In order to take full advantage of the code presented in this tutorial, you also need the Apache web server, because the sample application (a news management system) uses
mod_rewrite. The code from this tutorial is available for free download, so you can try it out for yourself. You can download it from the Brain Bulb web site at http://brainbulb.com/zend-framework-tutorial.tar.gz.
Downloading the FrameworkBefore you can get started with this tutorial, you need to download the preview release of the framework. You can do this manually by visiting http://framework.zend.com/download with a browser and choosing the
zipdownload, or you can use the command line:
$ wget http://framework.zend.com/download/tgz
$ tar -xvzf ZendFramework-0.1.2.tar.gz
librarydirectory and place it somewhere convenient. In this tutorial, I rename
libto provide a clean and simple directory structure:
wwwdirectory is the document root,
viewsare empty directories you'll use later, and the
libdirectory is from the preview release download.
Getting StartedThe first component I want to show you is
Zend_Controller. In many ways, it provides the foundation of the application you're developing, and it's also part of what makes the Zend Framework more than just a collection of components. Before you can use it, however, you need to direct all incoming requests to a single PHP script. This tutorial uses
mod_rewritefor this purpose. Using
mod_rewriteis an art in itself, but luckily, this particular task is very simple. If you're unfamiliar with
mod_rewriteor configuring Apache in general, create a
.htaccessfile in the document root and add the following directives:
RewriteRule !/.(js|ico|gif|jpg|png|css)$ index.php
Zend_Controlleris to remove the
mod_rewritedependency. In order to provide an example that works with the preview release, this tutorial uses
httpd.confdirectly, you must restart the web server. Otherwise, if you use a
.htaccessfile, you should be good to go. You can quickly test this by placing some identifiable text in
index.phpand making a request for an arbitrary path such as
/foo/bar. For example, if your host is
example.org, request the URL http://example.org/foo/bar. You also want
include_pathto include the path to the framework library. You can do this in
php.ini, or you can just put the following directive in your
php_value include_path "/path/to/lib"
Zendclass contains a collection of static methods that are universally useful. This is the only class you must include manually:
Zend.php, you have access to all of the methods from the
Zendclass. Loading other classes is simplified with the
loadClass()method. For example, to load the
include_pathaware, and it also understands the organization and directory structure of the framework. I use it to load all other classes.