Zend, the company behind the latest PHP-scripting engines, conducted a coding contest earlier this year. The winning project, Prado (developed by Qiang Xue), brings ASP.NET's server-side components (asp:TextBox, asp:Button), validators
(RequiredFieldValidator, CompareValidator) and server-side events handlers, to PHP 5. We assume
that you are familiar with ASP.NET and that you have an up and running Apache and MySQL on
PCQLinux 2004.
Install and setup
Download latest PHP 5 from www.php.net.
xtract the archive and compile and install.
./configure --prefix=/usr --with-mysql --with-zlib -with-apxs2
make
make install
Fire up a text editor and type in the following.
AddType application/x-httpd-php .php
DirectoryIndex index.php
Save the file as php.conf in /etc/httpd/conf.d. In case you had installed PHP that comes with PCQLinux 2004, you will already have a file named php.conf in this directory. In this case, open the file and comment the following line by prefixing a #.
LoadModule php4_module modules/libphp4.so
Restart Apache Web server.
/etc/rc.d/init.d/httpd restart
|
Now download Prado from www.zend.com/php5/
contest/download.php?id=36. and unzip it. This will produce a directory named prado. To develop a PHP Web application using Prado, you will need the subdirectories js and WEB-INF.
Say hello to Prado
To write a Web application using Prado, you have to write a PHP class for each Web page in your application. For example, for a Web page named Test, you need a file named Test.php containing a class definition as:
class Form extends TPage
This class must be specified in a .spec file which can be thought of as similar to a web.config file for ASP.NET. The .spec file can also be used to specify user defined parameters like the database credentials (as you will see later). The actual content for the page Test will be in a file named Test.tpl. Our example Web application presents a form which prompts for name, address, pin code and e-mail and stores it in
MySQL.
A Prado app
First, create a database in MySQL named app:
mysqladmin -u root -p create app
When prompted, give the MySQL root password. Next:
mysql -u root -p
Again, when prompted, supply the MySQL root password. You will be dropped onto a mysql> prompt. Here, issue the following to create a table named info.
create table info (id int auto_increment not null, name varchar(20),address varchar(50),pin varchar(10), email varchar(20), primary
key(id))
Now, create a directory named app under /var/www/html. Copy the subdirectories js and WEB-INF (found in the prado directory that you extracted) to this directory. Create a directory named pages under /var//www/html/app/WEB-INF. The .spec file will be under WEB-INF, while the PHP class file and the .tpl files will be placed under WEB-INF/pages. The .spec file for our application is:
In the
for class files corresponding to the pages and also the Prado-related classes. The latter two directories
contain the Prado-related classes. Under
HTML form while t 'submitted' will display "Database Updated" after the entries have gone into the
database. With we define the default or the first page to be shown to a user when he visits the URL of our Web application. In this case we want to first show the page containing the form. Within the
html/app/WEB-INF.
The Form.tpl file for the first page (named form) is as follows.
Enter the Info:
Name:
Address:
MaxLength="50" />
Pin:
E-Mail:
Save the above contents as Form.tpl under WEB-INF/pages directory.
In the above code as opposed to 'asp' prefix (called the namespace) in ASP.NET, the components (textboxes and buttons) and validators have a 'com' prefix. Also their name starts with a T. Barring these two differences, the name of the components remains the same as in ASP.NET. Note that "runat=server" is not required and does not exist in Prado. The use of validators like RequiredFieldValidator and CompareValidator remains the same. Prado provides a new validator for Email addresses, namely TEmailAddressValidator; the use is similar to the
RequiredFieldValidator.
The OnClick event for the submit button calls a function called submit(). This function is to be defined in the Form.php class as shown below.
class Form extends TPage
{
function submit($sender,$param)
{
if($this->IsValid)
{
$dbHost = $this->Application->getParameter("dbhost");
$dbName = $this->Application->getParameter("dbname");
$dbUser = $this->Application->getParameter("dbuser");
$dbPassword = $this->Application->getParameter("dbpassword");
// Code to connect to the database
$db_connection = mysql_connect($dbHost,$dbUser,$dbPassword);
$db = mysql_select_db($dbName,$db_connection);
$name = $this->name->Text;
$address = $this->address->Text;
$pin = $this->pin->Text;
$email = $this->email->Text;
// Code to query the database
$query = "insert into info (name,address,pin,email) values('$name','$address','$pin','$email')";
$query_result = mysql_query($query,$db_connection);
$this->Application->transfer("submitted",$param);
}}}
?>
Save the above as Form.php in WEB-INF/pages directory. $this refers to the page itself. The parameters defined in the spec files are accessed as:
$this->Application->getParameter("
After submitting the query to the database, the transfer function is used to load the next page, named as "submitted" in the spec file. The class file for the "submitted" page is as follows.
class Submitted extends TPage
{}
?>
Save the above as Submitted.php in WEB-INF/pages. The corresponding .tpl file is as follows.
Database Updated
Save the above as Submitted.tpl in the WEB-INF/pages directory. Finally we need to write a PHP file which will instantiate our Web application. For this save the following code as index.php in
/var/www/html/app directory.
require_once(dirname(__FILE__).'/WEB-INF/framework/TApplication.php');
$app = TApplication::getInstance('WEB-INF/app.spec');
$app->run();
?>
The client-side validation works only for IE. For more, refer to tutorial.pdf found in the extracted
prado/docs directory.
Shekha Govindarajan
IT4Enterprise