The PHP language has been a darling of the open source software development community for years for reasons such as performance, the power of the language itself, and ease of use. Now, PHP language with the frameworks being built around it, seems to be ready for enterprise software development. This chapter provides you concept and hands-on with Zend Framework for building enterprise Web applications that require database and application integration support. When deciding to build an On Demand1 Web application, there are many products and technologies that you can use. In most of the cases, you need to choose the following:
In this chapter, we do not address the hardware, operating system, or Web server software much. In fact, we do not need to. One of the strengths of PHP is that it is available on all of today’s popular operating systems, including many versions of UNIX and Linux operating systems. Similarly, IBM’s flagship database product, DB2, is also versatile and runs on Microsoft Windows, UNIX, Solaris, and many flavors of Linux. We have used the two most popular and widely used setups to showcase the application development steps in this chapter:
Irrespective of the Web server software and operating system you choose, you should seriously consider using PHP with DB2 Express-C for the following reasons:
Alternatively, you can use Zend Core for IBM, which packages the Apache Web Server, PHP libraries, and DB2 in one bundle. Currently, Zend Core include DB2 V8.2, so we recommend that you install DB2 9 first and then try to install Zend Core for IBM. Zend Core is supposed to find the DB2 already installed on your system and use it unless you choose to replace it. Remember that in order to take advantage of DB2 pureXML technologies and the XCS (discussed in this chapter), you must use DB2 9.
DB2 Express-C supports a full range of APIs for application development and simplifies the server side development with stored procedures and SQL functions. For PHP developers, DB2 Express-C comes bundled with Zend Core for IBM, a seamless, ready to use, easy-to-use PHP development environment.
DB2 Express-C includes leading edge technology for storing, managing, and searching XML data in a secure scalable environment. With its pureXML technology, your PHP application makes use of standard XQUERY and SQL/XML to build the latest applications.
Both PHP and DB2 Express-C are very fast and efficient. PHP Web pages can serve millions of hits per day using a single inexpensive server. DB2 Express-C is proven to be faster than its competition in most of the application development scenarios.
Both PHP and DB2 Express-C are available on many platforms. Your code usually works without modifications on different systems that run PHP and DB2.
Information on demand is about getting the right information to the right people at the right time to take advantage of the business opportunities. Both PHP and DB2 Express-C are extremely flexible in supporting the varying demand of business requirements. Also since Express C has been enabled to tightly integrate with other IBM Express products, you can leverage this to build scalable and resilient on demand solutions very quickly.
In the true spirit of the open source movement, we decided to build our sample application using Web Development Framework. In software development, a framework is defined as a support structure in which other software projects can be organized and developed. A framework might include support programs, code libraries, a scripting language, or other software to develop and glue together the different components of a software project. Here are few examples of application development framework. You can find more than 20 frameworks on the Web, written in PHP alone:
- Apache Cocoon and Apache Struts from Apache software foundation
- CakePHP, an open source framework written in PHP
- Zend Framework, an open source framework in PHP (preview release)
- Eclipse framework from Eclipse foundation
- .Net framework from Microsoft
- Ruby on Rails, an open source framework written in Ruby
- Net Beans, a Java-based Web application development framework by Sun Micro systems, Inc.
- Catalyst, an open source development framework written in Perl
- Django, an open source Web application framework written in Python
- Cocoa from Apple computer
A framework provides a skeleton on which you can build a software solution using different components. A framework also allows developers to focus on business logic and not the tedious part of software integration. Some people may argue that using a framework can add code bloat (unnecessary long and slow code) and also usually involves a steep learning curve. But the increasing popularity of PHP for database-driven Web applications is enough good reason for us to consider use of a framework for PHP development in this book. We decided to use an open source framework called Zend Framework for PHP. We divide this chapter into two parts.
Part I gives an overview of PHP application development using Zend Framework preview release, followed by a sample database application using Zend DB Adapter for access to DB2 Express-C.
Part II focuses on development of a PHP application using the new XCS (XML Content Store) technology, which is part of Zend Framework. This section is also followed by a sample, Social Network Application, which you can build and deploy very quickly using XCS and new DB2 Express-C XML pureXML technology.
Again, in the spirit of open source, we decided to use the Eclipse application development environment (with a PHP plug-in) to write PHP code. Zend also offers a trial version of its development environment called Zend Studio, but we chose Eclipse because it is free and easy to use.
Zend Framework overview
The PHP language has been around for a long time, but it was not adopted in large enterprise environments. It began as a scripting language and was considered as a glue to integrate processes quickly and flexibly. PHP is also popular for building database driven Web applications. With the development of application development frameworks, PHP is well prepared for enterprise applications because it should be capable of integrating components and services in the enterprise. Zend Framework is this type of framework developed by Zend and its partners for designing PHP5-based enterprise grade Web applications quickly. The framework is based on MVC (Model-View-Controller) architecture. The primary benefits of using a MVC architecture for your next Web application are:
- The same enterprise data can be accessed using different views. It accesses enterprise data through the model and specifies how that data should be presented. It is the view's responsibility to maintain consistency in its presentation when the model changes.
- The same enterprise data can be updated using different views.
- Core business logic is separated from the presentation layer and control logic. The controller translates interactions with the view into actions for the model to perform.
Zend Framework aims to provide an architecture for developing entire Web applications with no other library dependencies. Zend and its partner companies are committed to actively develop and support the framework code. Zend also supports a community Wiki page as a developer zone. This Web site contains recent committed code and also issues problem (bug) tracking and other developer resources. Zend Framework also enforces a strict PHP coding standard to maintain uniformity among the framework and application code.
The core of Zend Framework is the Zend_Controller. The Zend_Controller is designed to be light weight, modular, and extensible. The Zend_Controller workflow is implemented by several components. Understanding the underpinnings of all components is not necessary to build an application, but knowing how they work and interact with each other is definitely helpful:
This component processes all the requests received by the server and is responsible for delegating requests to action controllers.
This process takes the URI endpoint and decomposes it to determine which controller and action of that controller should receive the request.
The dispatcher process takes the dispatcher token, finds the appropriate controller, instantiates the controller class, and finally runs the action method in that controller object. In summary, this is how Zend_Controller works:
- A request is received by Zend_Controller_Front, which in turn calls Zend_ Controller_ Router to determine which controller (and action in that controller) to dispatch.
- Zend_Controller_Router decomposes the URI into a Zend_ Controller_ Dispatcher_ Token object that describes where to dispatch.
- Zend_Controller_Front then enters a dispatch loop. It calls Zend_ Controller_ Dispatcher, passing it the token, to dispatch to the actual controller and action.
- After the controller has finished, control returns to Zend_Controller_Front.
If the controller has indicated that another controller should be dispatched by returning a new token, the loop continues and another dispatch is performed. Otherwise, the process ends.
We discuss more about these objects whenever needed, but for now, let us go through the setup required for using Zend Framework.
Setting up Zend Framework
Setting up Zend Framework is quite easy. This section assumes that you have PHP up and running with some supported Web server in your environment. Our example uses:
- Apache HTTP server 2.0.58 and assumes it is installed in the C:\Program Files\Apache Group\Apache2 directory.
- IPHP 5.1.4 with the latest IBM DB2 driver for PHP.
- Directory structure
One very important goal of any framework is to simplify the common code structure. We recommend this directory structure:
- Save and unzip the Zend Framework preview code in the directory of your choice. When unzipped, it saves itself in ZendFramework-0.1.4 directory and contains demos, documentation, incubator, library, and tests directories along with a readme, news, and license files.
- Create a directory called zframework in the Apache Web server htdocs directory as follows:
- Copy the entire library directory from your Zend Framework directory to the zframework directory you just created. Rename this directory to lib.
- Create additional directories in zframework directory to organize your code. The directory structure should look as shown below
zframework directory structure
Note: Zend Framework is currently released as preview 0.1.4 and at the time of writing this book, this was the release available to use. Check the Web site and use the latest version as it becomes available. You can also connect to their Subversion Repository for anonymous checkout of the latest code and report bugs. Zend Framework Developer Web site has plenty of information for PHP developer.
- Web server configuratin
One important goal of modern Web applications (Web sites) is to have clean URIs (without any attached query parameters). This kind of URI rewrite support is required from Web server. Zend Framework currently depends on Apache’s mod_rewrite module to redirect all requests to a single file called index.php that resides in Web server’s document root directory and contains the bootstrap function (in this case, Zend_Controller_Front). The configuration of the server requires a few steps:
- The first step is to enable mod_rewrite function of Apache Web server. By default, this function is not enabled. To enable it:
After these changes, restarting Web server should enable the mod_rewrite.
- Uncomment LoadModule for mod_rewrite function in the Apache configuration file httpd.conf.
- You would also need to set the Document Root to .../zframework/www and change Allow Override value from NONE to ALL.
Note: Zend is currently looking for a solution, which does not require mod_rewrite, for the future. There is a good chance that the final release of this framework will not require you to perform this setup.
- Next, we need to set the rule to redirect all incoming Web requests to www/ index.php. You can easily do this by creating a .htaccess file in the www directory with the contents as shown below
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
The above file redirects all requests not containing one of the previous file exten- sions to index.php bootstrap file. Alternatively, you can modify httpd.conf also for the rewrite rules, but that requires you to restart the Web server. Adding to .htaccess file does not require you to restart Web server. You also need to include the include_path to the framework library directory (zframework/lib). You can add this to the php.ini file or simply add it into .htaccess as shown below
php_value include_path "C:\Program Files\
The Zend class (zend.php) contains static methods that are used by many classes. This is the only class you need to include manually, so we add it to our bootstrap file index.php.
After adding it to the bootstrap file, we now have access to all static methods. You can load other classes with the loadClass() method. For example, to load Zend_ Controller_ Front:
Note: This is the only php file required to exist within the Web server document root directory. Also for security reasons, it makes sense not to store the php files in the directories that are accessible by the Web server.
Now, to understand the framework flow and make sure setup is working, let us do this exercise:
- Edit your index.php to look as shown below
$dbuser = "db2admin";
$dbpass = "db2admin";
$dbname = "CONTACTS";
$params = array( 'username' => $dbuser,
'password' => $dbpass,
'dbname' => $dbname );
$conn = Zend_Db::factory('Db2', $params);
$db = new Database($conn);
$view = new Zend_View;
$view → setScriptPath('../app/views');
$controller = Zend_Controller_Front::getInstance()
Let us discuss the different parts here:
- First we include the zend.php. In PHP5 and above, you can define an autoloader function (__autoload) which is automatically called in case you are trying to use a class which has not been defined yet. The scripting engine is given a last chance to load the class with the __autoload function before PHP fails with an error.
- Next we create and instantiate a database connection. Zend_Db_Adapter provides the database API abstraction layer, which supports many SQL databases including DB2. You need to call Zend_Db::factory() with the adapter name (DB2 in this case) and a list of parameters with connection information. We now save this db adapter $db in the Zend registry variable so that we can access it in our application anywhere.
- Next we create an instance of Zend_View and set the path to the instance to tell where to look for view template files. Zend_View is a class for working with the "view" portion of the model-view-controller pattern. That is, it exists to help keep the view script separate from the model and controller scripts.
- The last section of index.php creates the instance of the front controller and specifies the directory where actual controller files are found. The framework wants you to create controller classes in app/controllers directory.
- Now let us try to point your Web browser to the Web server with some random path such as http://localhost:81/redbook/toc. In our case, you should see an error. We expect the error and here is why. Since we requested for redbook/toc, the framework looks for a controller called RedbookController.php in the app/controllers directory. Since it did not find any, it then looked for default IndexController.php. It did not find it either. Remember, every request is treated as controller/action in the framework. Index is the default for both the controller and the action. So now, let us create IndexController.php as shown below get going.
class IndexController extends Zend_Controller_Action
public function indexAction()
echo 'what should I do?';
There is another useful method called noRouteAction(). You can specify a _redirect() call to redirect the requests for nonexistent pages to the desired page. if you add the noRouteAction() method to the IndexController.php, it redirects our request of redbook/toc to the main page.
class IndexController extends Zend_Controller_Action
public function indexAction()
echo 'what should I do?';
public function noRouteAction()
$this → _redirect('/');
c. Now, let us create the RedbookController.php as shown below. We have an index Action() method for non-existent action request and tocAction() for our redbook/toc request.
class FooController extends Zend_Controller_Action
public function indexAction()
echo 'What should I do?';
public function tocAction()
echo 'TOC of this book follows';
public function __call($action, $arguments)
Now, our request for http://localhost/redbook/toc should echo the text TOC of this book follows. Notice there is another function, __call(), to handle undefined actions such as redbook/appendix.
You can see that the controller/action mechanism allows you to have friendly URIs for your Web application and also allows you to organize them properly. This completes our basic test of mod_rewrite and the framework setup. We discuss other controller objects in detail when we build our sample application but for now we move on with the Eclipse enviro- nment setup.