Zend Framework quick start

By Cynthia Sullivan,2014-01-27 08:50
52 views 0
Zend Framework quick start

Zend Framework Quick Start

    Table of Contents

    ; Zend Framework & MVC Introduction

    ; Create Your Project

    ; Create A Layout

    ; Create a Model and Database Table

    ; Create A Form

    ; Congratulations!

    Zend Framework & MVC Introduction Zend Framework

    Zend Framework is an open source, object oriented web application framework for PHP 5. Zend Framework is often called a 'component library', because it has many loosely coupled components that you can use more or less independently. But Zend Framework also provides an advanced Model-View-Controller (MVC) implementation that can be used to establish a basic structure for your Zend Framework applications. A full list of Zend Framework components along with short descriptions may be found in the ? components overview. This QuickStart will introduce you to some of Zend Framework's most commonly used components, including Zend_Controller, Zend_Layout, Zend_Config, Zend_Db, Zend_Db_Table, Zend_Registry, along with a few view helpers.

    Using these components, we will build a simple database-driven guest book application within minutes. The complete source code for this application is available in the following archives:

    ; ? zip

    ; ? tar.gz


    So what exactly is this MVC pattern everyone keeps talking about, and why should you care? MVC is much more than just a three-letter acronym (TLA) that you can whip out anytime you want to sound smart; it has become something of a standard in the design of modern web applications. And for good reason. Most web application code falls under one of the following three categories: presentation, business logic, and data access. The MVC pattern models this separation of concerns well. The end result is that your presentation code can be consolidated in one part of your application with your business logic in another and your data access code in yet another. Many developers have found this well-defined separation indispensable for keeping their code organized, especially when more than one developer is working on the same application.

    Note: More Information

    Let's break down the pattern and take a look at the individual pieces:

    ; Model - This is the part of your application that defines its

    basic functionality behind a set of abstractions. Data access

    routines and some business logic can be defined in the model.

    ; View - Views define exactly what is presented to the user.

    Usually controllers pass data to each view to render in some

    format. Views will often collect data from the user, as well.

    This is where you're likely to find HTML markup in your MVC


    ; Controller - Controllers bind the whole pattern together. They

    manipulate models, decide which view to display based on the

    user's request and other factors, pass along the data that

    each view will need, or hand off control to another controller

    entirely. Most MVC experts recommend ? keeping controllers

    as skinny as possible.

    Of course there is ? more to be said about this critical pattern, but this

    should give you enough background to understand the guestbook application we'll be building.

Create Your Project

    In order to create your project, you must first download and extract Zend Framework.

    Install Zend Framework

    The easiest way to get Zend Framework along with a complete PHP stack is by installing ? Zend Server. Zend Server has native installers for Mac OSX, Windows, Fedora Core, and Ubuntu, as well as a universal installation package compatible with most Linux distributions.

    After you have installed Zend Server, the Framework files may be found under /usr/local/zend/share/ZendFramework on Mac OSX and Linux, and

    C:\Program Files\Zend\ZendServer\share\ZendFramework on Windows. The

    include_path will already be configured to include Zend Framework. Alternately, you can ? Download the latest version of Zend Framework and

    extract the contents; make a note of where you have done so. Optionally, you can add the path to the library/ subdirectory of the

    archive to your php.ini's include_path setting.

    That's it! Zend Framework is now installed and ready to use. Create Your Project

    Note: zf Command Line Tool

    In your Zend Framework installation is a bin/ subdirectory, containing

    the scripts and zf.bat for Unix-based and Windows-based users,

    respectively. Make a note of the absolute path to this script. Wherever you see references to the command zf, please substitute the absolute path to the script. On Unix-like systems, you may want to use your shell's alias functionality: alias

    If you have problems setting up the zf command-line tool, please refer to the manual.

    Open a terminal (in Windows, Start -> Run, and then use cmd). Navigate to a directory where you would like to start a project. Then, use the path to the appropriate script, and execute one of the following:

    1. % zf create project quickstart

    Running this command will create your basic site structure, including your

    initial controllers and views. The tree looks like the following:

    1. quickstart

    2. |-- application

    3. | |-- Bootstrap.php

    4. | |-- configs

    5. | | `-- application.ini

    6. | |-- controllers

    7. | | |-- ErrorController.php

    8. | | `-- IndexController.php

    9. | |-- models

    10. | `-- views

    11. | |-- helpers

    12. | `-- scripts

    13. | |-- error

    14. | | `-- error.phtml

    15. | `-- index

    16. | `-- index.phtml

    17. |-- library

    18. |-- public

     | |-- .htaccess 19.

    20. | `-- index.php

     `-- tests 21.

    22. |-- application

    23. | `-- bootstrap.php

    24. |-- library

    25. | `-- bootstrap.php

    26. `-- phpunit.xml

    At this point, if you haven't added Zend Framework to your include_path,

    we recommend either copying or symlinking it into your library/ directory.

    In either case, you'll want to either recursively copy or symlink the library/Zend/ directory of your Zend Framework installation into the library/ directory of your project. On unix-like systems, that would look like one of the following:

    1. # Symlink:

    2. % cd library; ln -s path/to/ZendFramework/library/Zend .


    4. # Copy:

    5. % cd library; cp -r path/to/ZendFramework/library/Zend .

    On Windows systems, it may be easiest to do this from the Explorer. Now that the project is created, the main artifacts to begin understanding are the bootstrap, configuration, action controllers, and views. The Bootstrap

    Your Bootstrap class defines what resources and components to initialize. By default, Zend Framework's Front Controller is initialized, and it uses

    the application/controllers/ as the default directory in which to look for action controllers (more on that later). The class looks like the following:

    1. // application/Bootstrap.php


    3. class Bootstrap extends


    4. {

    5. }

    As you can see, not much is necessary to begin with.


    While Zend Framework is itself configurationless, you often need to configure your application. The default configuration is placed in application/configs/application.ini, and contains some basic directives

    for setting your PHP environment (for instance, turning error reporting on and off), indicating the path to your bootstrap class (as well as its class name), and the path to your action controllers. It looks as follows:

    1. ; application/configs/application.ini


    3. [production]

    4. phpSettings.display_startup_errors = 0

    5. phpSettings.display_errors = 0

    6. includePaths.library = APPLICATION_PATH "/../library"

    7. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"

    8. bootstrap.class = "Bootstrap"

    9. appnamespace = "Application"

    10. resources.frontController.controllerDirectory =

    APPLICATION_PATH "/controllers"

    11. resources.frontController.params.displayExceptions =



    13. [staging : production]


    15. [testing : production]

    16. phpSettings.display_startup_errors = 1

    17. phpSettings.display_errors = 1


    19. [development : production]

    20. phpSettings.display_startup_errors = 1

    21. phpSettings.display_errors = 1

    Several things about this file should be noted. First, when using INI-style configuration, you can reference constants directly and expand them; APPLICATION_PATH is actually a constant. Additionally note that there are several sections defined: production, staging, testing, and development. The latter three inherit settings from the "production" environment. This is a useful way to organize configuration to ensure that appropriate settings are available in each stage of application development.

    Action Controllers

    Your application's action controllers contain your application workflow,

    and do the work of mapping your requests to the appropriate models and views.

    An action controller should have one or more methods ending in "Action"; these methods may then be requested via the web. By default, Zend Framework URLs follow the schema /controller/action, where "controller" maps to the action controller name (minus the "Controller" suffix) and "action" maps to an action method (minus the "Action" suffix).

    Typically, you always need an IndexController, which is a fallback controller and which also serves the home page of the site, and an ErrorController, which is used to indicate things such as HTTP 404 errors (controller or action not found) and HTTP 500 errors (application errors). The default IndexController is as follows:

    1. // application/controllers/IndexController.php


    3. class IndexController extends Zend_Controller_Action

4. {


    6. public function init()

    7. {

    8. /* Initialize action controller here */

     } 9.


    11. public function indexAction()

    12. {

    13. // action body

    14. }

    15. }

    And the default ErrorController is as follows:

    1. // application/controllers/ErrorController.php 2.

    3. class ErrorController extends Zend_Controller_Action 4. {


    6. public function errorAction()

    7. {

    8. $errors = $this->_getParam('error_handler');


    10. switch ($errors->type) {

     case 11.



    12. case



    13. case




    15. // 404 error -- controller or action not


    16. $this->getResponse()->setHttpResponse


    17. $this->view->message = 'Page not


    18. break;

    19. default:

    20. // application error

    21. $this->getResponse()->setHttpResponse


    22. $this->view->message = 'Application


     break; 23.

    24. }


    26. $this->view->exception = $errors->exception;

    27. $this->view->request = $errors->request;

    28. }

    29. }

    You'll note that (1) the IndexController contains no real code, and (2) the ErrorController makes reference to a "view" property. That leads nicely into our next subject.


    Views in Zend Framework are written in plain old PHP. View scripts are

    , where they are further categorized placed in application/views/scripts/

    using the controller names. In our case, we have an IndexController and an ErrorController, and thus we have corresponding index/ and error/

    subdirectories within our view scripts directory. Within these subdirectories, you will then find and create view scripts that correspond to each controller action exposed; in the default case, we thus have the view scripts index/index.phtml and error/error.phtml.

    View scripts may contain any markup you want, and use the opening

    tag and ?> closing tag to insert PHP directives.

    The following is what we install by default for the index/index.phtml view







Welcome to the "zf-name">Zend



This is your project's main page





    LightBG.png" />




    42. Helpful Links:

    43. Zend

    Framework Website |




    45. Manual

Report this document

For any questions or suggestions please email