Application environment - IBM DB2

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

Why framework?
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:

  • Zend_Controller_Front
  • This component processes all the requests received by the server and is responsible for delegating requests to action controllers.
  • Zend_Controller_Router
  • This process takes the URI endpoint and decomposes it to determine which controller and action of that controller should receive the request.
  • Zend_Controller_Dispatcher

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:

  1. 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.
  2. Zend_Controller_Router decomposes the URI into a Zend_ Controller_ Dispatcher_ Token object that describes where to dispatch.
  3. 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.
  4. After the controller has finished, control returns to Zend_Controller_Front.
  5. 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:

    1. 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.
    2. Create a directory called zframework in the Apache Web server htdocs directory as follows:
    3. Copy the entire library directory from your Zend Framework directory to the zframework directory you just created. Rename this directory to lib.
    4. 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:

    1. The first step is to enable mod_rewrite function of Apache Web server. By default, this function is not enabled. To enable it:
      • 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.
    2. After these changes, restarting Web server should enable the mod_rewrite.

      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.

    3. 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

    .htaccess file

    RewriteEngine on

    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

    include_path

  • Zend class
  • 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:

    1. Edit your index.php to look as shown below
    2. index.php

      Let us discuss the different parts here:

      1. 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.
      2. 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.
      3. 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.
      4. 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.
    3. 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.

    IndexController.php

    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.

    Redirect request

    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.

    RedbookController.php

    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.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

IBM DB2 Topics