Chin Lee

Home

Creating Zend Framework Project from Scratch

Published Mar 21, 2012

Requirements

  • 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:

<?php
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));

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

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

require_once 'Zend/Application.php';

$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
            ->run();

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:

[development]
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:

<?php
class IndexController extends Zend_Controller_Action {
  public function indexAction() {
  
  }
}

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

<?php
class ErrorController extends Zend_Controller_Action {
  public function errorAction() {
  
  }
}

Create the Default Template

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

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Zend Framework Tutorial</title>
  <?php echo $this->headLink()->appendStylesheet('//twitter.github.com/bootstrap/assets/css/bootstrap.css') . PHP_EOL; ?>
  <?php echo $this->headScript()->appendFile('//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js') . PHP_EOL; ?>
</head>
<body>
  <div class="container">
    <div class="row">
      <div class="span12">
        <p>This is the header!</p>
      </div>
    </div>
    <div class="row">
      <div class="span12">

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


        <hr />
        <footer>
          <p>This is the footer!</p>
        </footer>
      </div>
    </div>
  </div>
</body>
</html>

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

<?php
include_once('header.phtml');
echo $this->layout()->content;
include_once('footer.phtml');

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 http://www.example.com/zf-project/ and will see a plain and clean website like this:

Example 01

Source Code

Project source code available in GitHub for your convenience.

Conclusion

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