Zend Framework Quick Start. Create Your Project

Translations of this material:

into Russian: 1.2 Zend Framework — Быстрый старт. Создание проекта. Translation complete.
Submitted for translation by antdmi 07.02.2011 Published 7 years, 8 months ago.


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 zf.sh 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 zf.sh=path/to/ZendFramework/bin/zf.sh.

    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:

  01. % 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:

  01. quickstart

  02. |-- application

  03. |   |-- Bootstrap.php

  04. |   |-- configs

  05. |   |   `-- application.ini

  06. |   |-- controllers

  07. |   |   |-- ErrorController.php

  08. |   |   `-- IndexController.php

  09. |   |-- models

  10. |   `-- views

  11. |       |-- helpers

  12. |       `-- scripts

  13. |           |-- error

  14. |           |   `-- error.phtml

  15. |           `-- index

  16. |               `-- index.phtml

  17. |-- library

  18. |-- public

  19. |   |-- .htaccess

  20. |   `-- index.php

  21. `-- tests

  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:

  01. # Symlink:

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


  04. # Copy:

  05. % 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:

  01. // application/Bootstrap.php


  03. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap

  04. {

  05. }

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:

  01. ; application/configs/application.ini


  03. [production]

  04. phpSettings.display_startup_errors = 0

  05. phpSettings.display_errors = 0

  06. includePaths.library = APPLICATION_PATH "/../library"

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

  08. bootstrap.class = "Bootstrap"

  09. appnamespace = "Application"

  10. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"

  11. resources.frontController.params.displayExceptions = 0


  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:

  01. // application/controllers/IndexController.php


  03. class IndexController extends Zend_Controller_Action

  04. {


  06.     public function init()

  07.     {

  08.         /* Initialize action controller here */

  09.     }


  11.     public function indexAction()

  12.     {

  13.         // action body

  14.     }

Pages: ← previous Ctrl next
1 2 3