PRADO: PHP goes ASP.NET (ish)
August 4, 2008 – 11:53 am Tags: PHPLately, 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.












8 Responses to “PRADO: PHP goes ASP.NET (ish)”
I’ve worked with ASP.NET webforms in the past, and all I can say is why would you want this? It seems like a step backwards to me, introducing this nasty (and leaky) abstraction over the web. If you are a web programmer, imo it is important to understand the web, including things like POST and so on. This just sweeps it all under the covers and pretends it doesnt happen - just like ASP.NET webforms. Yuck.
By Mark on Aug 4, 2008
While it does that, I think it can be quite convenient when you don’t have to deal with all the things that are going on.
I probably wouldn’t use PRADO for big projects, but it shows different ways to think about common scenarios, which makes it interesting.
The question really is how far would (should?) you go with abstraction like this? If you could define events in a similar fashion, but without the leaky and nasty abstraction - say with some routing magic which is automatically parsed by a controller - would it be better? I think it might, but I don’t know if any framework actually does something like that out of the box yet, at least in this way.
By Jani Hartikainen on Aug 4, 2008
I have been using ASP.NET for 6 years, and there are a lot of things I like about it and the underlying .NET framework. However, I can say unequivocally, webforms is NOT one of them. Microsoft is now introducing an alternate framework that is MVC based, that does not try to abstract away all the underlying statelessness and web centric technology the way webforms does. Mark hit the nail on the head when he called it a leaky abstraction, because that’s exactly what it is.
Microsoft is backing away or at the very least giving an alternative to webforms now that we’re on version 3.5 of asp.net. Learn from the mistakes of others.
By Fregas on Aug 4, 2008
Does it at least provide xsd or dtd for templates? Because, as I can see, PHP code completion kind of poor.
By Alan on Aug 4, 2008
I’ve been working with Prado for two years now, and I’m really happy with it. It’s been a major productivity improvement in all the projects I’ve used it for.
When it comes to the pagestate this can be stored in a database (included component), which would vastly improve performance. The JS bits can be a drawback, but we are in 2008.. are there really any browsers not supporting javascript anymore?
By Eirik Hoem on Aug 26, 2008
Eirik: “…are there really any browsers not supporting javascript anymore?”
Me: Yes, and plenty more to come in the handheld / mobile market. Think smaller, not bigger.
Anyway, I just saw Wikipedia deleted the PRADO entry they had and was searching the web for references to PRADO to try to get the article reinstated. If you care, contact Wikipedia about it.
By Ken Krauss on Oct 28, 2008
I’ve been using PRADO for a while, and following its development since its conception AGES ago, and all I can say is good things about it.
The only drawback I’ve found (and it’s really fixable, if you really know what your are doing and you’re not a script-kiddie or copy+paste maniac) is the performance penalty that it imposses… but in this era of DO IT QUICK, it’s the best framework out there for RAD in php.
For speeding PRADO performance, see this link which is very usefull:
http://www.eioba.com/a77342/prado_performance_tuning_for_high_traffic_web_applications
By EdwinF on Dec 2, 2008