jump to navigation

Creating a simple project with the Zend Framework : part1 April 15, 2011

Posted by Tournas Dimitrios in Zend Framework v1.10.

This article is a step by step presentation how to create a simple project with the Zend Framework . It’s only targeted to newcomers , so experienced users will not benefit  from this article . The reader must be familiar with basic concepts of PHP , he must already known how to set-up a Mysql database (creating  – deleting – updating  tables etc… ) , via command line or a  web-interface like phpMyadmin . Some basic skills , how to configure the development environment  are also a must (only a few modifications on the php.ini  and httpd.conf files ) . The final result of the first part of this article will be a web-page that contains a footer , header with some links to internal / external pages . Future articles will enrich this project with more features like log-in , authentication and database access .

The best way to learn is with practice , so follow along . Of course you can download the project files from here , but use these files just for reference .

This is a picture of the final project :

Let’s get started :

These are the steps that must be followed :

  1. A development environment , Wamp – Mamp or Lamp .
  2. Initialize your Zend Framework , read my article here .
  3. Configure a Virtual host on your web server .
        ServerName learningzend.local
        DocumentRoot “C:/program files/wamp/www/learningZend/public”
      SetEnv APPLICATION_ENV “development”
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
  4. Configure your local name-resolution file   zendtest.local
  5. Restart your server and check ,  with your browser  , that your Zend Project is accessible via your browser :
  6. Creating our layout : Instead of each action view script needing to include site-wide artifacts, they can simply focus on their own responsibilities. The layout will contain the header , footer and the link buttons .

    • Open your terminal window , navigate to your project’s root folder and type the following command
      zf  enable layout
      The following lines inside the main configuration file /application/configs/applicaton.ini  of your project are added automatically and application/layouts/scripts/layout.phtml is created .
      resources.layout.layoutPath = APPLICATION_PATH “/layouts/scripts”

  7. A good development practice is that all  html files  contain XHTML DocType declaration , this can be enable in the bootstrap file /application/bootstap.php

    • class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
      protected function _initDoctype()
      $view = $this->getResource(‘view’);
  8. Initialize a View resource object . This step is needed from the _initDoctype() method from previous step , so add the following configuration option in application.ini file
    resources.view[] =
  9. Configuring Custom route : That makes it possible to map application URL’s to specific modules , controllers and actions . Simply add the following lines on the application’s configuration file . A route entry contains route , module , action and controller attributes .
    resources.router.routes.home.route = /home
    resources.router.routes.home.defaults.module = default
    resources.router.routes.home.defaults.controller = index
    resources.router.routes.home.defaults.action = index
    Now the URL http://learningzend.local/home will navigate to the central home page vs index/index (index controller/ index action ) .

    A more practical way to define custom routes (especialy if the list get’s very long ) , is to use the Zend_Controller_Router_Route constructor . Read my article .

  10. Configuring Routes for static content pages . Add the following route definitions to the application central configuration file (application.in ) .
    resources.router.routes.static-content.route = /content/:page
    resources.router.routes.static-content.defaults.module = default
    resources.router.routes.static-content.defaults.controller = static-content
    resources.router.routes.static-content.defaults.action = display
    The Zend Framework supports the use of variable placeholders , it is indicated by a preceding colon in our route pattern ” :page” . 
  11. Writing the layout file : Open the file from application/layout/scripts/layout.phtml
    The code in this file defines the header , footer and link buttons
  12. Create a controller that will handle the static content pages
    zf  create controller StaticContent  index
  13. create in the directory application/view/scripts/action-content  all your static pages vs about , services , sitemap ……
  14. The StaticContent  controller must contain the following method :
    public function displayAction()
          $page = $this->getRequest()->getParam(‘page’);
              if (file_exists($this->view->getScriptPath(null) . “/” . $this->getRequest()->getControllerName() . “/$page.” . $this->viewSuffix)) {
          } else {
            throw new Zend_Controller_Action_Exception(‘Page not found’, 404);
    This method is responsible to read the page link variable from the routing subsystem and match it with a corresponding view script (static page )  . If no matching can be found , an exception is produced and a error page is displayed to the client .

Links must see !!! :


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s