Chin Lee


Creating Zend Framework Project from Scratch

Published Mar 21, 2012


  • A working Apache web server
  • mod_rewrite enabled
  • PHP 5.2+

Download latest Zend Framework from Zend Framework website and extract to your working directory.

Create the Project Directory

First, create a new directory in your working directory for the project and navigate into it.

$ mkdir zf-project
$ cd zf-project

Next, we will need to create a directory application to hold all the controllers, models, views and configs, etc.

Also create a .htaccess in the root like this:

SetEnv APPLICATION_ENV "development"

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ index.php [L,QSA]

And create an index.php in the root like this:

    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));

    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../../zf/library'),

require_once 'Zend/Application.php';

$application = new Zend_Application(
    APPLICATION_PATH . '/configs/application.ini'

Create the Project Structure

Create these directories and sub-directories in application/ directory:

  • configs
  • controllers
  • models
  • modules
  • views
    • scripts
      • error
      • index
    • template
$ mkdir application/configs
$ mkdir application/controllers
$ mkdir application/models
$ mkdir application/modules
$ mkdir application/views
$ mkdir application/views/scripts
$ mkdir application/views/scripts/index
$ mkdir application/views/scripts/error
$ mkdir application/views/template

Create the Application Configurations File

Create config application.ini under application/configs/ directory:

phpSettings.display_startup_errors = 1
phpSettings.display_errors         = 1
phpSettings.DATE.timezone          = "Asia/Singapore"

resources.frontController.controllerDirectory      = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath                        = APPLICATION_PATH "/views/template/"

[staging : development]
phpSettings.display_startup_errors = 0
phpSettings.display_errors         = 0

[production : development]
phpSettings.display_startup_errors = 1
phpSettings.display_errors         = 1

Create the Application Controllers

Create controller IndexController.php under application/controllers/ directory:

class IndexController extends Zend_Controller_Action {
  public function indexAction() {

Create controller ErrorController.php under application/controllers/ directory:

class ErrorController extends Zend_Controller_Action {
  public function errorAction() {

Create the Default Template

Create header header.phtml under application/views/template/ directory:

<!DOCTYPE html>
  <meta charset="utf-8" />
  <title>Zend Framework Tutorial</title>
  <?php echo $this->headLink()->appendStylesheet('//') . PHP_EOL; ?>
  <?php echo $this->headScript()->appendFile('//') . PHP_EOL; ?>
  <div class="container">
    <div class="row">
      <div class="span12">
        <p>This is the header!</p>
    <div class="row">
      <div class="span12">

Create footer footer.phtml under application/views/template/ directory:

        <hr />
          <p>This is the footer!</p>

Create template layout.html under application/views/template directory to combine both header and footer:

echo $this->layout()->content;

Create the View for Controllers

Create view index.phtml for IndexController under application/views/index/ directory:

<h2>Welcome to Zend Framework Tutorial!</h2>

Create view error.phtml for ErrorController under application/views/error/ directory:

<h2>An error occurred!</h2>

Project Ready

You can now browse to and will see a plain and clean website like this:

Example 01

Source Code

Project source code available in GitHub for your convenience.


You can create projects with Zend Framework easily with Zend Framework Console Tool. Check Documentation: Using the CLI Tool for more information.