Tutorial MVC pattern for Zend Framework

I like to use the Zend Framework (currently v1.0.0 RC 2) and I like to use a Model-View-Controller (MVC) pattern to divide my business logic, my visual stuff and my own scripts. What I didn't find on the net was an actual example that would show you how to set up a basic application using the ZF and MVC, with the ZF standards in mind.
So, for my own reference (and the rest of the world) I write here a simple application using Zend Framework and the Zend Framework MVC model.

This tutorial uses LAMP settings, so for other setups you need to figure some stuff out yourself (and add them here as comments ;-)

My setup:
  • Linux: 2.6.18
  • Apache: 2.2.3
  • MySQL: 5.1
  • Php: 5.2.1
  • Zend Framework: 1.0.0 RC 2
Step 1: Getting the Framework
Go to www.zendframework.com and download the latest version of Zend Framework. For easy maintenance, unpack the files in the /opt directory using version info in the directory (e.g. /opt/zend_framework_1.0.0_rc2).
Symlink /opt/zf to /opt/zend_framework_1.0.0_rc2, so you can switch easily to newer versions.
dev@system: # ln -s /opt/zend_framework_1.0.0_rc2 /opt/zf

Step 2: Setting your Apache server
We use a virtual host for our development
<virtualhost *:80>
  ServerName demo.example.com
  ServerAlias demo
  DocumentRoot /srv/www/htdev/demo
  <Directory /srv/www/htdev>
    Options Indexes FollowSymlinks
    AllowOverride All
    Order deny,allow
    Allow from, localhost
    DirectoryIndex index.php index.html

Step 3: setting up a development environment
ZF recommends the following default layout:

Step 4: Create the bootstrap
Adjusting /htdocs/.htaccess with the following lines:
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

Your bootstrapfile is /htdocs/index.php. This is where all requests pass thanks to the adjustments in the .htaccess file.

//include path to zend framework and models
set_include_path('.' . PATH_SEPARATOR . '/opt/zf' . PATH_SEPARATOR . '../application/models/');

//timezone and time options


//include the Front controller to handle all business logic
require_once 'Zend/Controller/Front.php';

//set the controller directory

This is the basic you'll need to start running your ZF MVC.
In the next post I write a little application using this setting.


  1. Anonymous22/6/07 12:12

    I think it's better to use this in your .htaccess

    RewriteEngine on
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule .* index.php

    This way you simply direct everything to to index.php unless it's a file or a directory. So you don't have to change your rule if you want to serve another filetype from the filesystem.

  2. Anonymous23/6/07 12:16

    Zend Framework Manual gives both options as a possibility, so depending on you needs you can use one or the other.


Post a Comment

Popular Posts