21.5 PHP for Application Servers


Today application servers are widely used to make the development of complex, content-oriented Web sites easier. For a long time application servers were available for Java and Python (for example, Zope) only. Recently, PHP has become more and more popular as well, so a variety of application servers were introduced. In this section we will try to provide an overview of what you can do with application servers and how to use them efficiently to solve everyday problems.

21.5.1 Application Server Fundamentals

When talking about application servers, people often ask me what application servers are. Well, the answer to this question is slightly more difficult than answering the question "What is an atomic bomb?". Let's try to define the technical term application server: An application server can be seen as a framework that helps you to build content-oriented Web sites. Usually an application server consists of a set of classes that have a certain purpose and that fulfill certain tasks. Normally these tasks are basic operations. Imagine a table consisting of various columns. In many cases a user wants to sort the elements in a certain column. An application will most likely help you to implement this feature easily. To make it short: An application server is a set of classes. But what makes an application server an application server if it is just a set of libraries, functions, methods, and classes? Tough question. The answer is that usually libraries are limited to a certain functionality related to a certain topic. In the case of application servers, this is a little different because a variety of features are supported and all classes are optimized to work with each other. This is the main difference between a set of loose libraries and an application server.

In this chapter we will try to introduce you to some different concepts and approaches used by recent application servers based on PHP.

21.5.2 Simple Applications with PHPLens

One PHP-based application server is PHPLens, which you can download from http://www.phplens.com. PHPLens is a powerful framework for data-oriented Web sites. It is easy to use and works reliably. Although PHPLens is not free software, we have decided to include this here because it is a good example of a PHP-based application server.

21.5.2.1 Installing PHPLens

The first thing you have to do when working with PHPLens is to download the software from www.phplens.com. Several types of licenses are available. For this book Natsoft has given us a temporary advanced license. At this point, we would like to say thank you for the support we have received from the company.

After downloading and extracting the sources of PHPLens in the directory containing the HTML code (in this example we have used /usr/local/apache/htdocs/php), you can create a database:

 [hs@duron hs]$ createdb phplens CREATE DATABASE 

In this example, the database is called phplens. In the next step you have to install the Zend Optimizer. This tool can be downloaded from www.zend.com. Just register on the Web site and you can get the tool free. Follow the instructions for installing the software, and within two minutes the Zend Optimizer will be up and running.

After that PHPLens must be configured to your needs. The first thing to do is to configure the database the application server should access. Therefore rename sample-phplens.config.inc.php in the config directory to phplens.config.inc.php. In this file you have to change the settings from MySQL to PostgreSQL as shown in the next listing:

 if (!isset($PHPLENS_SESSION_DRIVER)) {         $PHPLENS_SESSION_DRIVER='postgres';     // database driver to use         $PHPLENS_SESSION_CONNECT='localhost';   // server address         $PHPLENS_SESSION_USER ='hs';    // userid         $PHPLENS_SESSION_PWD ='';       // password         $PHPLENS_SESSION_DB ='phplens'; // database (optional for some dbs) 

In this scenario the user who connects to the database on the local machine is called hs. As you have seen before, the database is called phplens. After defining the database, you can insert the data structure needed by the application server into the database. You can easily do this with the help of psql:

 [hs@duron php]$ psql phplens < phplens.sql NOTICE:  CREATE TABLE/PRIMARY KEY will create implicit index 'phplens_pkey' for table 'phplens' CREATE NOTICE:  CREATE TABLE/PRIMARY KEY will create implicit index 'sessions_pkey' for table 'sessions' CREATE 

If no errors occurred, a set of tables has been added to the database. In the next listing, you can see a list of these tables as well as the data structure of the new objects:

 phplens=# \d     List of relations    Name   | Type  | Owner ----------+-------+-------  phplens  | table | hs  sessions | table | hs (2 rows) phplens=# \d phplens                  Table "phplens"  Attribute |           Type           | Modifier -----------+--------------------------+----------  id        | character(12)            | not null  lastmod   | timestamp with time zone | not null  data      | text                     | not null Index: phplens_pkey phplens=# \d sessions            Table "sessions"  Attribute |     Type      | Modifier -----------+---------------+----------  sesskey   | character(32) | not null  expiry    | integer       | not null  data      | text          | not null Index: sessions_pkey 

As you can see, the tables contain just a few columns. The image in Figure 21.13 shows what comes out when you execute testlens.php.

Figure 21.13. Testing PHPLens.

graphics/21fig13.jpg

21.5.2.2 PHPLens Fundamentals and Examples

Now that you have installed PHPLens, it is time to write some code. You can create a directory so that the code of PHPLens is not mixed up with your code:

 [hs@duron php]$ mkdir code 

After that you can insert some data into the database. The next piece of code shows how to create a table for storing information about employees. Finally six records are added to the table:

 CREATE TABLE employee (id int4,         name text,         gender char(1),         income numeric(9,2)); COPY employee FROM stdin; 1       Peter   m       42000 2       Shelley f       59000 3       Epi     m       20000 24       Horst   m       39500 5       Ellen   f       43250 6       Kuli    m       12000 \. 

Let's take a look at a short PHP script using PHPLens:

 <?php         include('../phplens.inc.php');         session_start();         $lens = PHPLensConnect('emplLens', 'SELECT * FROM employee',                         'postgres', 'localhost', 'hs', '', 'phplens');         print "state of object: ".PHPLensLastState('emplLens')."<br><br>\n";         $lens->Render();         $lens->Close(); ?> 

First a library is included. This is essential if you want to use PHPLens because otherwise the system won't find the libraries included in the application server. Now a session is started. This is essential as well because internally everything is done by using session variables. PHPLensConnect creates a new object and returns the handle to this object. The name of this object is empLens. Be careful that every object has a unique name because otherwise strange things will happen. As you can see, a connection to the database is established and the result of a SELECT statement is assigned to the object. As soon as the render function is called, the HTML code generated by PHPLens is sent to the browser. Finally the connection is closed and the object is destroyed.

The image in Figure 21.14 shows what comes out when you execute the code.

Figure 21.14. A simple example.

graphics/21fig14.jpg

The main advantage of an application server is that many things are done by it automatically. In this example it is possible to sort the data in a column without writing a single line of code. When you click on the header of the table, you will see that the data is sorted as shown in Figure 21.15.

Figure 21.15. Sorting data.

graphics/21fig15.jpg

Now that you have seen how to build a simple application with the help of PHPLens, it is time to take a look at a slightly longer application that shows the basic principles of PHPLens:

 <?php         include('../phplens.inc.php');         session_start();         $lens = PHPLensConnect('emplLens', 'SELECT * FROM employee',                         'postgres', 'localhost', 'hs', '', 'phplens');         $lens->nameLens="id^serial";         $lens->pageSize = 3;         $lens->detailLens = 'id;name;';         $lens->Render();         $lens->Close(); ?> 

Before discussing the code, it is useful to take a look at the result.

As you can see in Figure 21.16, the title of the first column in the table has changed. This is done using the nameLens method.

Figure 21.16. A more powerful screen.

graphics/21fig16.jpg

In the next step, the size of a page is limited to three. This way PHPLens makes sure that the result is displayed using multiple pages. One line after that, a detailed lens is created. This lens contains detailed information about the columns listed on the right side of the = symbol.

The sample code you have just seen even has an additional feature. At the end of the script the data is sent to the server and the object is deleted.

As you have seen in this section, it is an easy task to write applications using PHPLens. Basic components such as tables can easily be created and modified to your needs. With just a few lines of code you can write simple yet useful applications. Although the code is only 10 lines long, it offers tremendous power. Let's click on an e in the detailed lens. The result is shown in Figure 21.17.

Figure 21.17. Configuring a lens.

graphics/21fig17.jpg

Many parameters can be configured to your needs.

21.5.3 Other Application Servers

A variety of additional application servers for PHP are available. One of the most powerful ones is called JPETO and can be downloaded from www.jpeto.com. To a large extent JPETO is built on nested objects. Here is a list of the most important features:

  • Multilanguage support

  • Caching objects

  • WYSIWYG preview

  • Session management

  • Sorting objects

  • Import/export functions based on XML

  • Interface to any ANSI-SQL compliant database such as PostgreSQL

JPETO can be used to build flexible and easy-to-use content management tools. For sites that require high performance, the cache manager will lead to significantly higher speed and to lower response times.

Another application server is called LXP. It is based on XML and can be used for a variety of tasks.



PHP and PostgreSQL. Advanced Web Programming2002
PHP and PostgreSQL. Advanced Web Programming2002
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 201

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net