PRADO: PHP goes ASP.NET (ish)

Tags:

Lately, I’ve been checking out PRADO, which is “a component-based and event-driven programming framework for developing Web applications in PHP 5”.

What that actually means is that you can use components, such as a data grid or a button, and work with them based on events without having to think about the traditional things associated with such: Parsing out POST or GET variables and handling the application state.

I think it’s a quite refreshing approach compared to what most PHP frameworks do.

Structure

PRADO uses an MVC-like pattern, so it’s quite easy to pick up if you’re familiar with some other MVC framework. You have the main “application entry point” or the front controller, the controllers/actions, views and an ActiveRecord implementation, amongst other things.

PRADO relies heavily on XML configuration files for providing application behavior: Defining modules, defining who can access which pages, defining site-wide layouts, user managers and so on. For example, where Zend Framework tends to favor code over configuration, PRADO lets you do a lot by simply modifying the XML. Still, it seems it’s quite extensible despite this.

Components and events

What sets PRADO apart from most other frameworks is the way the components and events work.

Each page in PRADO usually consists of two files: the template and the page class. The template contains the HTML code and component tags, the page class defines functionality in PHP code, such as handlers for component’s events, or loading data for the data table when the page is opened.

In a “traditional” approach, you would utilize POST or GET parameters and parse them yourself to figure out what the user just did. In PRADO, you tell the framework which method of the page class you wish which action to invoke and it handles the rest.

For example, if you have a form, you might check if the request was POSTed and then call some function. In PRADO, you simply tell the component that you wish it to call method X.

For example, the following code would define page “MyPage” which contains an HTML button which when clicked would change its text to “You clicked me”

MyPage.php

<?php
class MyPage extends TPage
{
    public function buttonClicked($sender, $param)
    {
        $sender->Text = 'You clicked me';
    }
}

MyPage.page

<html>
    <body>
        <h1>Component/Event demo</h1>
 
        <com:TForm>
            <com:TButton Text="Click Me" OnClick="buttonClicked" />
        </com:TForm>
 
    </body>
</html>

As you can see, you need very few lines to achieve this. In addition to these two files, you’d need to set up index.php with maybe three lines and application.xml, which could be pretty much done with some delicious copypasta.

Compare to say Zend Framework, you’d need to at least set up Zend_Loader, Zend_Controller_Front, write a controller action that looks if the request was POST and then assigns a view variable which is used in the view script to change the value attribute of the input… you can pretty quickly see that PRADO’s approach can save a lot of time.

But is it all good?

No. It would’ve been amazing if it was, but PRADO does have some things that you could consider flaws.

Similar to ASP.NET, PRADO needs to keep track of the page state between requests to keep all the event magic working. This can either be done with sessions or hidden forms that are submitted on all page loads. Also, the event magic for forms related things require a hidden field with a long postback string that identifies what’s going on etc. etc., which could get quite large if you have lots of components on the page. This is at least in the default implementation – the docs do say you could replace it with your own.

There are also some components which use a bit questionable JavaScripting to perform their stuff. For example, the TLinkButton uses an HTML anchor element to submit a form, but it relies completely on the availability of JavaScript. No JS, no go.

On the good side, the JavaScript code generated by the framework isn’t too bad. It uses Prototype and Scriptaculous on the back and seems to be quite well cross-browser compatible.

I personally didn’t like the ActiveRecord implementation much, but at least it seems it’s not required – I just decided to use Doctrine instead and everything worked just fine.

These are however the only drawbacks I can think of at the moment. Granted, I haven’t used PRADO for that long yet, but it has a good feel to it overall. It gives you a quite good array of built in components and things like built in caching support, authentication etc., and the event-based model is very refreshing for a change.

I would definitely recommend at least checking out and playing with PRADO a bit. It can give you some new ideas on how to approach things in PHP.