Professional Documents
Culture Documents
Web Helper: PHP Application Development With Prado
Web Helper: PHP Application Development With Prado
WEB HELPER
The Prado PHP development framework helps you quickly build web
G
oogle Docs and other online pro- tions. The letters of the name Prado model (model-view-controller). Each
ductivity tools reveal the poten- stand for “PHP Rapid Application Devel- Prado application consists of one or
tial for capturing the look and opment Object-oriented.” more templates that define the appear-
feel of a desktop application through the Prado addresses many facets of appli- ance of the page.
web programming model. Keeping the cation development, providing GUI com- A PHP file provides the program logic.
application on the server minimizes con- ponents that range from simple input Each template can include code files or
figuration and provides a more optimal fields to a wizard interface. other template files.
use of system resources. But the Soft- The XML template language included An example application that imple-
ware as a Service model is not confined with Prado offers an easy syntax that is ments a web-based Advent calendar
to huge and complicated applications on easily managed through normal HTML illustrates how easy it is to program with
the servers of global giants like Google. editors. All of the form components in Prado. The doors of the calendar open
Your software service also can be a cus- Prado keep track of their own status with a mouse click (Figure 1). Doors that
tom web application running at the local without the devel-
level. Developers use languages like PHP oper having to
to create small and efficient server-side worry about them.
programs that serve up results in a Prado also sup-
browser window. ports client-side
For efficient web application develop- validation control-
ment, it makes sense to use the building lers that check
blocks of a development framework. input data before
Several PHP frameworks are available the form is sent.
for Linux users – some of which were
discussed in the February 2008 “PHP Component
Scripting” issue of Linux Magazine. Design
One example of this new breed of PHP The architecture
development tools is the Prado frame- of the Prado Figure 1: A simple interactive application: the doors of an Advent
work [1], an extensive and mature de- framework is calendar open with a mouse click; The prize appears in the white field
velopment environment for web applica- based on the MVC below.
have already been opened remain open, loop. That is, it iterates the enclosed text [4]). In the example, the repeater repeats
so the application must keep track of the according to the number of data records lines 11-14.
door status. The bordered box under the from the data source linked in from the
doors indicates the contents of the calen- controller code. The variable $this in this Data Sources
dar compartment. context references the repeater. Lines 23-27 (Listing 2) links the repeater
The application should reuse as much Every Prado repeater provides the at- and the data source. In both cases, $this
code as possible; the code for the doors, tribute Data[], which is an array contain- references the current class, and thus the
as well as the view and the controller ing the data set from the correct iteration page object. Within a page object, the
components, should appear only once. of the loop. implemented methods are on par with
Because of limited space, only one door Prado repeaters offer more functional- Prado tags, which are indicated by the
per week is shown. ity than simple foreach() loops in PHP: ID attribute. Thus, $this->Repeater
Header and footer areas are displayed points to the repeater, which is indicated
The First Step only once. Only the area between in Home.page with the attribute ID=
To install the Prado framework on a web <prop:ItemTemplate> and </prop: "Repeater". The attribute DataSource of
server, you only need to unpack Prado ItemTemplate> is repeated by the frame- the Prado class Repeater contains the
in the web server’s root directory. Prado work based on the data set. data. The code assigns to it the return
applications presuppose a specific direc- If a block is marked with <prop:Ite- value of the getData() method.
tory structure in which only a central mAlternatingTemplate>, the contents of More demanding web applications get
index file is accessible. The actual code ItemTemplate and ItemAlternatingTem- their data from a database, which Prado
resides under the protected directory, plate alternate. This results in lines in a links in through its Object Relational
which is blocked by the .htaccess file for table that alternate color (see the first ex- Mapper or the ActiveRecord calls. In the
the web server. ample in the Prado QuickStart Tutorial example, the data originated from an
A command-line PHP script is started
by the developer from the directory in Listing 1: View Component of the Advent Calendar
which the application resides, using the 01 <!--- Form --->
name of the application as a parameter.
02 <com:TForm>
This script then creates the necessary
directory structure. 03 <!--- Repeater ("Datagrid") --->
A pair of files in the protected/pages 04 <com:TR ID="Repeater"
directory – called Applicationname.page 05 OnItemCommand="open_door" > <!--- all Events from the Repeater
and Applicationname.php – represents --->
the view and controller components of
06 <!--- Iterated code: Image, label and hidden field --->
the new application. Listings 1 and 2
show the contents for the Advent calen- 07 <prop:ItemTemplate>
dar application. 08 <div style="float:left">
The view component (Listing 1) 09 <com:TImageButton ID="door" ImageURL="door.png" />
defines an HTML form in the first line.
10 <div align="center">
Lines 8-14 create a door of the calendar.
11 <com:TLabel ID="tbox"
The template tag <com:TImageButton
text="<%# $this->Data['text'] %>" />
... > (line 9) links in the picture of the
closed doors. 12 </div>
A div block with the CSS Style float: 13 </div>
left attribute ensures that the compart- 14 <com:THiddenField ID="inside" data="<%#
ments of the Advent calendar line up $this->Data['inside'] %> " />
correctly. Template files can contain both
15 </prop:ItemTemplate>
Prado tags (recognizable by the prefix
<com:) and normal HTML tags. 16 <!--- End Iterated code --->
17 </com:TRepeater>
Multiplicator 18
An additional div tag below the picture
19 <!--- Display area for the contents of the calendar --->
centers the date text. The included PHP
code is responsible for the text, which 20 <div style="clear:left;border-width:2px; border-style:solid;
changes by day. The framing tags <%# background-color:#ffffff;
... %> tell Prado the included text is 21 width:650px; height:100px; text-align:center; padding:20px;
PHP source code and not template code. border-color:black;">
To understand this, it is necessary to 22 <com:TImage id="content" /> <!--- Bild --->
take a look at the <com:TRepeater> tag
23 </div>
in line 4. A repeater tag, borrowed from
Microsoft ASP.Net, resembles a foreach() 24 </com:TForm>