Added by Bradley Holt, last edited by Bradley Holt on Mar 08, 2008  (view change)

Labels

 
(None)

This Quick Start is not exactly "completed" but it is as close as I was able to get it by the deadline. I hope it has some material that is useful in creating the final Quick Start.

Zend Framework QuickStart

Introduction

Zend Framework is a "leading open-source PHP framework that has a flexible architecture that lets you easily build modern web applications and web services." Zend Framework is often referred to as a "glue" framework because it has many components that can be used to help build your web application. However, Zend Framework also provides an MVC "stack" that you may choose to use. This quick start guide assumes you are using Zend Framework MVC and will also cover the Zend_Controller, Zend_View, Zend_Layout, Zend_Form, and Zend_Db components.

Quick Start

Create the file system structure

For this quick start we will use QuickStart as our top-level project directory. Create the basic file system structure:

Note that this file system structure is based on the default project structure proposal. The file system structure above only includes the directories we will use for the rest of this Quick Start section. In the Next Steps section we will be adding additional directories as needed. One of the things you will discover about Zend Framework is that it is very flexible and extensible. There is nothing special about this particular file system structure other than the fact that it is considered a Zend Framework best practice and that various Zend Framework components will assume this structure unless otherwise configured.

Install Zend Framework

Install Zend Framework to the library directory. Your library directory should now contain the top-level Zend directory which contains all of the Zend Framework components. You may also want to install other vendor libraries in the library directory and/or your own library that you re-use from project to project. For more information on installing Zend Framework see the Installation section of the Reference Guide.

Set your document root

In your web server configuration, set your document root to QuickStart/public/. For this quick start we will assume this is the document root for http://www.example.com. You may have noticed that in the file system structure we talked about earlier we created css, js, and images directories. These are there simply to organize your static website files and it is up to you if you need them or not.

Note

Talk more about, or provide links to resources about, configuring document root.

Create your rewrite rules

Zend Framework MVC uses the Front Controller pattern. This means we need to rewrite all incoming requests (except those for static resources) to a single PHP script which will then appropriately route the request. Assuming you are using the Apache web server, create the file QuickStart/public/.htaccess with the following content:

This rewrite rule will rewrite all requests for files that do not end in .js, .ico, .gif, .jpg, .png, or .css to index.php.

Notes
  • Provide more information about how the Front Controller pattern works in Zend Framework.
  • Talk about alternatives for non-Apache web servers.

Create your index and bootstrap files

Create the file QuickStart/public/index.php with the following content:

Note that we have left out the closing ?> PHP tag. This is intentional and is a good practice to avoid accidental output of whitespace. Now we need to create our bootstrap file, QuickStart/application/boootstrap.php with the following content:

The bootsrap file does exactly what it says, bootstraps your web application. First we add our QuickStart/library/ directory to the include path. Next we run Zend_Controller telling it where to find our action controllers. Remember that QuickStart/public/index.php is the main script that is running so relative paths are relative to it, thus the '../library' and '../application/controllers' paths above.

Note

Provide more information about action controllers.

Create your index controller

In Zend Framework MVC the default action controller is index and the default action is index. Create QuickStart/application/controllers/IndexController.php with the following content:

Requests for http://www.example.com will be routed to indexAction in this action controller. Requests to http://www.example.com/index and http://www.example.com/index/index will also be routed to the same place. Note the camel case naming convention and that action controller names should end with Controller and action names should end with Action. Later we may want to add some functionality to indexAction but for now we will leave it empty.

Create your index view script

By default Zend Framework uses PHP for templating view scripts but it is possible to use other templating engines. It is recommended you use the .phtml extension so that you can distinguish view scripts from other PHP files. Zend Framework looks in QuickStart/application/views/scripts/ for a directory by the same name as the action controller and then within that directory for a file by the same name as the action with a .phtml extension. This behavior, like most everything in Zend Framework, is completely configurable. Since we want to create a view script for the index action within the index action controller we need to create the QuickStart/application/views/scripts/index/ directory (matching the name of the action controller) and then the QuickStart/application/views/scripts/index/index.phtml file (matching the name of the action) with the following content:

Note that we have to repeat all of the HTML in each of our view scripts. This is not optimal and we will talk about a solution to this using Zend_Layout in the Next Steps section.

Create your error controller

The default behavior of Zend Framework is to look for an ErrorController with an errorAction to handle application errors. Create QuickStart/application/controllers/ErrorController.php with the following content:

Create your error view script

Create the directory QuickStart/application/views/scripts/error/ and then the file QuickStart/application/views/scripts/error/error.phtml with the following content:

Summary

You should now have the following file system structure:

Visiting any of the following URLs will execute IndexController::indexAction() and display the contents of the index/index.phtml view script:

If you visit a URL that does not have a controller and action mapped to it (any other URL at this point) will execute ErrorController::errorAction() and display the contents of the error/error.phtml view script.

Download QuickStart.zip. This file includes all of the code created up to this point but does not include Zend Framework itself.

Next Steps

Using layouts

Zend_Layout gives us an implementation of the Two Step View pattern. Practically speaking, this allows us to create one site template, or layout, that all of our pages will use. This can give us a consistent look-and-feel between pages as well as allow us to follow the DRY (Don't Repeat Yourself) design principle. See the Zend_Layout Quick Start for more advanced usage.

Create your layout view script

Create the directory QuickStart/application/layouts/ and the file QuickStart/application/layouts/layout.phtml with the following content:

This will serve as our main website template. The PHP code $this->layout()->content is using the layout view helper to output the value of the content variable. Zend_Layout automatically assigns the value of the default segment to the content variable.

Notes
  • Explain what view helpers are.
  • Explain named segments.

Modify index and error view script

Since we will be using QuickStart/application/layouts/layout.phtml as our website template we need to strip out all but the actual content from our index and error view scripts. Modify QuickStart/application/views/scripts/index/index.phtml, stripping out all but the actual content so it looks like this:

Modify QuickStart/application/views/scripts/error/error.phtml, stripping out all but the actual content so it looks like this:

Modify bootstrap file

Now we need to modify our bootstrap file to start Zend_Layout MVC. Update QuickStart/application/bootstrap.php:

Note

By default Zend_Layout looks in QuickStart/application/views/scripts/ for layouts. The default project structure proposal puts layouts in QuickStart/application/layouts/. For this reason, we need to pass the layoutPath configuration parameter to Zend_Layout::startMvc(). This conflict between the default project structure proposal and Zend_Layout should be resolved and this quick start updated.

Create a header and footer

Lets create two more layout view scripts. First create the file QuickStart/application/layouts/header.phtml with the following content:

This will act as our navigation bar giving us one file to update when we add new items. Now create the file QuickStart/application/layouts/footer.phtml with the following content:

Grab a copy of the Powered By Zend Framework Logo and put it in QuickStart/public/images/. Update QuickStart/layouts/layout.phtml:

Download QuickStartLayout.zip. This file includes all of the code created up to this point but does not include Zend Framework itself.

Using Zend_Form

See the Zend_Form Quick Start.

Note

This should be filled in with an example of using Zend_Form within a Zend Framework MVC application.

Using Zend_Db

See Zend_Db.

Note

This should be filled in with an example of using Zend_Db_Table, Zend_Db_Table_Row, and Zend_Db_Table_Rowset within a Zend Framework MVC application.