Using Prado – A PHP Framework

Introduction

Recently, I was thinking about a new website idea. This website should be based around word games and let users buy the words used in the games for advertising. The website would be written using the PHP and MySQL. One thing I knew from the beginning, there would be lots of programming involved. Usually I am a person who likes to do everything himself. If I have to develop a non-standard web application I would often decide to write the whole thing myself instead of using an existing CMS or such and writing a plugin.

But this project was going to be slightly larger, and writing everything myself from scratch would have taken far too long. So I decided to use a PHP framework. PHP frameworks are a collection of code, meant to make your life easier and to aid you in the rapid development of dynamic websites. My choice fell on the Prado framework (see www.pradosoft.com).

Although the learning curve for using frameworks can be quite steep at times, the choice to use a framework paid off very quickly. I was able to implement the website which I had in mind in a fraction of the time that it would have taken me to write the whole thing from scratch. You can see the result at www.planetofwords.co.uk

Why I chose Prado

There are a multitude of PHP frameworks out there, and I had to make a decision about which one I wanted to use. I found a nice site comparing lots of different frameworks at phpframeworks.com and what intrigued me was that there were two frameworks following the Event Driven Programming (EVP) paradigm. Being used to developing software with Java and C++, an event driven architecture seemed quite natural to me. I will explain later on what EVP exactly means. One of the frameworks was Prado which I finally decided was going to be the framework of my choice.

Prado, like many other frameworks, supports the Model View Controller (MVC) design. This is neat, because the code is nicely separated from the design. In fact, in Prado, your design goes into special template files. These files don’t usually contain any PHP code. Instead they extend HTML with a number of XML tags and dynamic content tags. The XML tags represent components, such as buttons and hyperlinks, but also more complex components such as wizard interfaces of date pickers. There is no limit to the number of components, as you can easily extend Prado by writing your own components. As the name suggests, the dynamic content tags on the other hand can be used to put dynamic content onto your page. In most circumstances this means that the tag is replaced with the value of some PHP property. Finally, I found Prado to have a very extensive online documentation. They have the compulsory API docs, a forum that is very alive and a very comprehensive QuickStart Tutorial.

Of course I could now say that Prado is the best framework around but that would not be honest since I have not really tried other frameworks. I do think that Prado is definitely one of the better frameworks out there.

That said, it would not be fair to keep quiet about some negative issues that I came across. First of all, I found the configuration files a bit intimidating at first. Until I got to grips with the application configuration, I managed by copying and modifying the configuration files of one of the examples. Also, when I started using the Ajax features I found that the QuickStart Tutorial hadn’t been completed. So I had to bite my way through the API documentation to try to figure out how the Ajax controls work.

Maybe the largest disadvantage of Prado is that it needs PHP 5 to run. So you must make sure that your server not only supports PHP 5, but also handles .php file with PHP 5. Often the server default is to handle .php files with PHP 4, and if you want to make use of PHP 5, the files have to have the .php5 extension. In this case it might still be possible to use Prado, by adding the following lines to your .htaccess file.

AddType x-mapp-php5 .php
AddHandler x-mapp-php5 .php

This will map the .php file extension to PHP 5.

Finally, a very important point to consider when using a framework is the speed. While Prado is certainly not lightweight, it does support caching. I really can’t comment on how fast Prado is in comparison with other frameworks, since I have not tested that, but Prado applications can be run in a number of settings such as debug, normal and performance. These settings represent different levels of caching.

A First Example

OK, let’s look at the basics of how to create a page. I will not go into the details of setting up and configuring Prado. This would not fit into the scope of this article. The output of a Prado application is based on page templates. Usually, you will only need to define a single page template for your application. A vary simple page template could look like this:

File: Layout.tpl

<html>
<com:THead Title=<%$ SiteTitle %> >
<com:TStyleSheet StyleSheetUrl=&quot;screen.css&quot; />
</com:THead>
<body>
<com:TContentPlaceHolder ID=&quot;Main&quot; />
</body>
</html>

You can see some special tags. The tag represents the part of the HTML page. The tag is a placeholder for the page content. This is where the content of the individual pages will be rendered. (Note: You can have multiple tags defined in a layout as long as their IDs are unique)

Basically, a page consists of two files. The first file has the ending .page and contains the HTML code that defines the layout of the page. The second file is a .php file containing a PHP class that controls the page. Apart from the endings, both files have to have the same filename. The PHP class also has to have the same name as the filename.

So let’s look at the .page file first. This looks similar to the layout template above. Here is an example of a simple login form

File: Login.page

<com:TContent ID=&quot;Main&quot;>
<com:TForm>

<h1>Login</h1>

<p>Username<br/>
<com:TTextBox ID=&quot;Username&quot;/></p>

<p>Password<br/>
<com:TTextBox ID=&quot;Password&quot; TextMode=&quot;Password&quot;/></p>

<com:TButton ID=&quot;LoginButton&quot; Text=&quot;Login&quot; OnClick=&quot;loginButtonClicked&quot; />
</com:TForm>
</com:TContent>

The tag tells the framework that this content is substituted for the TContentPlaceHolder in the layout whit the “Main” ID. The is the equivalent of a form. You should use this if you want the full functionality of Prado, because this tag adds a number of hidden fields. In the same way, stand for a HTML input field and stands for an HTML button. You will see that the TButton has an attribute called OnClick. This attribute is set to a function name in the PHP code. To see how the page template interacts with the PHP code let’s look at the corresponding class file.

File: Login.php

<?php
class Login extends TPage
{
public function loginButtonClicked($sender,$param)
{
$username = $this->Username->Text;
$password = $this->Password->Text;

... do authentication and redirect on success ...
}
}
?>

When the login button is clicked, the loginButtonClicked function of the PHP class is called. Technically speaking, an event is raised, which is sent to the page object. The loginButtonClicked function has been linked to the event handler and thus gets called when the event is raised. Inside the class you have access to all the field values by simply using their IDs as class members. $this->Username refers to the TTextBox< ?code> with the ID Username. Being an input field, the text box has a property called text which represents the content of the input field.

That’s it. You can see that code and design are very neatly separated, and with the event driven architecture it is child’s play to handle requests.

The above example is a very simple one. You can have multiple buttons and links inside a form that call different function in you class. You can add validators, that automatically check the input of form fields and display an error message if, for example, the input field is left empty.

Ajax Support

One of the nicer features of Prado is the support of Ajax. This means that events can be raised using JavaScript and the page content can be modified depending on the result of the server sided script. I used this in my project, when people add words to their shopping cart. I wanted to achieve the same kind of feel that you get on Fotolia. When the Add to Cart link is clicked, the word should be added to the cart by the PHP code. On success the Add to Cart text should then change to In Your Cart. So let’s see how this is done. The page contains the following code beneath the word

<com:TActivePanel ID=&quot;ToCart&quot; >
<p class=&quot;action&quot;>
<com:TActiveImageButton ImageUrl=&quot;images/tocart.png&quot; onClick=&quot;onToCartClicked&quot;/>
<com:TActiveLinkButton onClick=&quot;onToCartClicked&quot;>Add to Cart</com:TActiveLinkButton>
</p>
</com:TActivePanel>

This displays an image of a shopping cart and a link saying Add to Cart. Both of these are contained inside an TActivePanel. Prado uses the “Active” in an elements name to show that is an Ajax enabled element. When either of these is clicked, an Ajax request is sent to the server and the onToCartClicked function is called. This function looks like this

public function onToCartClicked($sender, $param)
{
... add word to shopping cart ...

if ($success)
{
$this->CallbackClient->addCssClass($this->ToCart,'inactivelink');
$this->CallbackClient->replaceContent ($this->ToCart,'<p class=&quot;action&quot;>In Your Cart</p>');
}
}

When the word is successfully added to the shopping cart, the content of the active panel is modified and a CSS class is assigned to it. The text now reads In Your Cart and the CSS class makes the text appear slightly faded. This signals the user an inactive link.

I found it extremely easy to develop Ajax enabled applications. Behind the scenes Prado uses the Prototype and Scriptaculous libraries. This means you can make use of all the Scriptaculous effects like fading in and out of elements etc.

Conclusions

Prado is a nice PHP framework. It made the development of a relatively large website possible for me. If I had developed the whole website from scratch without a framework, it would have probably taken me at least twice as long. Without using a framework, I would not have dared to include as much Ajax in the website as I have now.

This is the first website that I developed using a PHP framework and I don’t regret my choice. While Prado is the only framework I have gained experience with, I am quite thrilled by it. I will definitely never develop a PHP website again without using a PHP framework. And unless someone can come up with very convincing arguments, I will certainly stick with Prado.

20 Comments on "Using Prado – A PHP Framework"

  1. Nice article! I might have to look into Prado some more!

  2. Great! I am definitely going to check out Prado.

  3. gik says:

    how can you write about any framework without trying others? without any context and comparison

  4. AntonioCS says:

    I haven’t read all of your blog post but I stopped after reading this: “Maybe the largest disadvantage of Prado is that it needs PHP 5 to run”

    PHP4 has been discontinued. I see no reason for you to want a framework that works with php4. But if you really want php4 try using cakephp

  5. Well-written intro – could have used a more detailed overview of the structure of the system, but still solid enough to entice those who would dare take the leap.

    @gik: There’s no need for context, as this was an intro to and feedback on a specific CMS, period.

  6. @gik: I am not comparing Prado to any other framework. I have browsed through the documentation of some other frameworks and I found Prado the most promising to get started on. In this article I am just presenting my experiences.

    @AntonioCS: While PHP5 is standard now and is supported by MOST servers, there is still a lot of PHP4 code out there. If you want to work with some additional library your options will be limited. I think this is a disatvantage in terms of compatibility.

  7. Luke Dingle says:

    Hey, nice article.

    I’ve used and read about so many frameworks over the last year and I’ve never see much about Prado.

    I’ll have to look into it sometime soon.

  8. astrologer says:

    If it had not been for Mysql, I would have got interested in Prado as it seems to be a good application, but, in fact, I avoid everything connected with Mysql.

  9. If you are looking at PRADO, i suggest you look at Yii (see http://www.yiiframework.com) it is written by the same guy that did PRADO but a much faster MVC style framework.

  10. Simon Harry says:

    Very well-written article, everything’s explained in a coherent and simple way. Nice job.

    I’m considering Prado for a site I’m building and was wondering what IDE, if any, you used to build your site with prado?

    Cheers.

  11. reddy says:

    i’m using prado to develop my website, but i’ve got some problem when i make ‘search’ function. i hope i can finish it in two weeks….

  12. Can YILMAZ says:

    Very nice intro. I’m using CodeIgniter (see codeigniter.com) for building large websites. I also checked out Prado from their site. It remembered me ASP.NET style. Instead of <asp: you write <com:. I know it's a really good framework, but not for me :)

  13. DevCom says:

    Thank you to make me feel good, and feel continue to use Prado.
    I have used Prado since 2006, i found that i like it very much. it separates nicely Interface Design and PHP Code, easy to understand. i have taught Prado to more than 20 people in my country. And i am sure that there is only me that use Prado so i am developing my Prado group in my country.

    Cheer.

  14. I have used Prado for a number of projects now and I love it. It is a bit tricky when you first start using it, but once you have got your head around how it works there is no doubt that it is a good framework. Highly recommend it over writing native php, but I have not tried any other frameworks.

  15. Oh – I meant to mention to the guy who said he didnt like something associated with MySQL – I use Prado with SQLServer with my own database access classes – I dont use any of Prado’s data access stuff. This is by choice – not because the Prado stuff doesnt work.

  16. Scott Deagan says:

    Thank you so much for sharing your experiences with Prado. I am currently considering learning a PHP framework in order to become more productive, and have narrowed my choices to PRaDO or Yii.

  17. Nilesh Tighare says:

    Nice Article…
    I am also interested in PRADO framework because of its simplicity, event driven programming like ASP.Net, AJAX support, mvc code standards.

  18. Sri says:

    Can someone tel me how to run the prado framework ?? Should i go for Netbeans or Eclipse to run it??

    Cant i run it thro command line??

    Googled n got this
    php path/to/framework/prado-cli.php but of no use and i don knw how to use this

    Can someone help???

  19. Abe@DevCom says:

    Dear Sri,

    I would like to write down some comment.

    !!!! Install WAMP on drive C or D.

    1. Download Prado Framework from pradosoft.com now 3.1.7

    2. Extract and rename it to be easier and copy prado folder to document root ( ex: c:\wamp\www\prado\

    3. For testing that you can use prado just open a web browser and type //localhost/prado to view Prado introduction

    4. Getting start on Menu Getting Quick Tutorial

    5. For Text editor use can use Dreamweaver i install Prado Extension we can download from version 3.1.4

    And restart your dreamweaver

  20. This is a nice article..
    Its very easy to understand ..
    And this article is using to learn something about it..

    Thanks a lot..!

Got something to say? Go for it!