Getting Started With Zend Framework

The Zend Framework library is based around the MVC (Model, View, Controller) and Front Controller design patterns. The Front Controller is the point of entry for all requests that are NOT being served up directly by your web server. Such requests include style sheets, javascript, images and other static files. Zend Framework implements the front controller pattern with a number of subcomponents. The main components are the router and the dispatcher. The router determines what action to take and the dispatcher runs the requested action or actions.

The MVC portion of the framework is where we create the code needed for our application. The model part contains the bulk of the business-logic code. Tasks such as retrieving and storing data in a database are done by the model. Zend Framework provides the Zend_Db_Table component for easy table-level interaction with the database. Other components that make developing the model portion of MVC easy are provided by Zend_Service. The View is the display logic. This is where the templates containing HTML code for your website is displayed. The controller contains the rest of the code that makes up the application.

To get started, we’ll download the latest Zend Framework release. Zend Framework assumes that the library directory is available in the php_include path. I prefer to store ZF outside of this to keep environments isolated from one another. (eg. /var/www/ We’ll also need to add some rewrite rules to our httpd.conf file. If your running Ubuntu or other Debian derivative it should be located at /etc/apache2/httpd.conf

<Directory /var/www/>
RewriteEngine on
RewriteRule !^.*..*$ /index.php

What this rule is saying is that if the request does not contain a dot . send it to our controller. This will allow any static file request to bypass the controller and be delivered up by the web server directly. There are other rules that could be used here such as checking if %{REQUEST_FILENAME} exists, is a directory or is a link with a rewrite condition then proceeding to the rewrite rule if none of these conditions apply. To me this seems a bit excessive as more CPU and I/O time will be spent on each request to validate these conditions.

Next we need to create our bootstrap file. This file initializes and configures our application. This will be the only PHP file needed in our public directory so it is often named index.php.

set_include_path(ROOT_DIR . '/library' . PATH_SEPARATOR . get_include_path());
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Controller_Front');// set up the front controller
$frontController = Zend_Controller_Front::getInstance();
$frontController->setControllerDirectory(ROOT_DIR . '/application/controllers');

With Zend Framework’s front controller, the dispatcher expects to find a file called IndexController.php in the /var/www/ directory.

class IndexController extends Zend_Controller_Action
    public function indexAction()
        $this->view->assign('title','Hello World!');

The indexAction() function assigns the ‘title’ varaible to the view property by the action helper Zend_Controller_Action_ViewRenderer. The ViewRenderer looks in the /var/www/ directory for a template file named after the action with a .phtml extension, within a folder named after the controller. This means it will look for /var/www/

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<html dir="ltr" lang="en">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<?php echo $this->escape($this->title); ?>
<?php echo $this->escape($this->title); ?>

The helper function escape() ensures that output is HTML safe. That is, it helps secure your site from XSS (cross site scripting) attacks. You should now be able to load up your site and be greeted with Hello World! via the Zend Framework.

Be the first to write a comment.

Your feedback