To support simplicity, possiibility and cost effective of PHP

Writing the Hello world application with Prado

Jun 26th, 2008 | By admin | Category: Prado framework

1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
Loading ... Loading ...

Prado PHP framework is a really cool framework, especially for .NET programmers who are looking for solutions in PHP. Prado focus mainly in making things done than doing a single task smartly. But coder won’t have to worry about quality of their code as Prado classes are written so well already.

I’ve spent more than one year with Prado, seeing the community grows day by day and people who I am talking too most say Prado is good and somehow, better than Zend framework or widely use CakePHP, Code Igniter.

As Prado document is not very easy to learn and their tutorials uses SqlLite as the database, most beginners are reluctant to learn it. That’s why I create this series of Prado training.

The Prado Hello, world

In this app, we learn some common .NET concepts in Prado. You should have first LAMP enviroment setup and Prado 3.1.2 (latest version when this article written) downloaded.

Setup your project

Extract the /framework folder to your disk as the Prado application need it. I will put it at /www/framework and my appliation will be at /www/hello. This is for reference in the next step.

Create your project folder structure using this command line:

php path/to/prado-cli.php -c hello

Note that you should stand at /www to execute this command so that Prado create the /www/hello folder for you.

prado-cli.php also provides some usefull command-line tool like generating active-record classes from Database. You will see it later.

Folder structure

Like any PHP framework, Prado creates the folder structure for your application.

  • protect : is where code goes in
  • assets : Prado resource files created at runtime. Must has chmod 777
  • protected/runtime: Prado runtime folder contains application state, cache data, etc.
  • protected/pages: configurable, store all web pages

We will look at application.xml deeper in another article but first you need to know that it contains application configurables information.

<service id=”page” class=”TPageServiceDefaultPage=”Home” BasePath=”Application.pages”>

</service>

This shows some glues to you:

  • BasePath: point to protected/pages folder. Here note that Prado uses . path syntax and ‘Application’ is mapped to folder protected.
  • DefaultPage: Home is the file /protected/pages/Home.page. When you site runs, the URL is
    …/hellow/index.php?page=Home. However, the Home page is the default page, you don’t have to specify page paramenter.
  • <service> tag is used to register the service to handle user requests. Depend on the id value,Prado load the service to handle user request. Here, when the first parameter on the querystring is page, Prado load an instand of TPageService to handle request.

OK, don’t wanna make you more confused about Prado configuration at this moment. Let’s go to the page and write some code.

Event based programming

Prado application handle user requests by handling the events. This is very similar to .NET web application where you have pages and a page has events such as Init, Load, PreRender, etc. Each controls in the page also has events. When user interacts with control, it will raise the event and you handle that event in the code to response to user request.

You will have to create  Home.php file in protected/pages folder if you don’t have one. The standard code template for a page class looks like this:

class Home extends TPage
{
  public function onLoad($param)
  {
  }
}
 

You can see the function OnLoad($param). This is where we write code to initialize the page whenever it’s requested by users.

Because Prado separates PHP code and HTML template, there must be ways for your code to pass data to your template, here is the Home.page file. Open it and edit the file:

<html>
<body>
<com:TForm>
  <com:TLabel Id="lblInfo" Text="Hello" />
  <com:TButton Text="Click me" OnClick="buttonClicked" />
</com:TForm>
</body>
</html>

You can run your app now and see the Hello text and the button Click me next to it. Click the button and you get an error message. This is normal as we haven’t declare the buttonClicked function to handle click event. Prado is Event based!!!

Our mission is 50% complete as you see the word Hello. Let’s do the 50% remain.

Add this code below function OnLoad in Home.php file:

protected function buttonClickec($sender, $param)
{
$this->lblInfor->Text = 'Hello, world';
}

Retest the page and you should now see expected result. Easy! isn’t it.

What we did is creating a function to handle an Prado control event. An event handler needs two params, $sender represents the object which fire the event and $param contains event data. Most of the time, you don’t care much about $param b/c changed data reflects in the control properties but for some events, you will have to learn what contained in the $param object/array. You have var_dump() and print_r() PHP functions with you, or you have Prado manual as your companion.

Talking about way to pass data from code to template, you can see that we access the label control  whose Id is lblInfo using this syntax: $this->lblInfo. You can write this because Prado will parse the template file before processing the php file. During this time, Prado creates propeties represents controls in the template.

A Prado control has many properties, Text is a common property for TLabel, TButton and TTextBox. Again, open Prado manual to learn more about Prado standard controls.

That’s all for a classic application. Now you know how to create a Prado project using prado-cli.php command line tool, see how a page is served and the events raised by a page or a control. In the next article, we will look into some Prado module, work with MySQL database using Prado ActiveRecord, ActiveRecord Scaffolding and SqlMap. We will also take a look at a very powerful control that Prado “borrows” from ASP.NET, the Repeater control, to show tabular data.

Tags:

One comment
Leave a comment »

  1. Thank you for this small tutorial!

Leave Comment