Building Flex application for a PHP class using Flash Builder 4

June 1, 2009

Updated for Flash Builder 4 release version :)

If you have a PHP class, Flash Builder 4 allows you to create a Flex front end for it with just few clicks. Continue reading this article to find out how.

In this article we will be creating Flex application which will invoke functions in CustomerService class which will return Customer class objects for each entry in the database. We will use Flash Builder DCD to create a Flex application which will consume the Customer objects returned by the CustomerService class.

Install Flash Builder 4

Download and install Flash Builder 4 from this URL http://www.adobe.com/products/flashbuilder/

Create a new Flex project

Create new Flex project from File -> New -> Flex Project

Enter project details as shown in the image above and explained below

In this screen:

  1. Set the project name to “ExistingPHPSample”
  2. Set the Application type to “web (run in Adobe Flash Player)”
  3. Use the default Flex SDK (Flex 4.0)
  4. Set the Application server type to “PHP”
  5. Click next to continue

In this screen:

  1. Set the Web root to the root folder of your PHP server. Its c:\apacheserver\htdocs in this sample
  2. Set the Root URL to the root URL of your PHP server. Its http://localhost:9292 in this sample
  3. Leave the Output folder to default value
  4. Click the “Validate Configuration” to validate the server details entered
  5. Click on finish to continue

Using DCD to consume data returned by the PHP file

DCD or Data-Centric Development is one of the advancements to the Flash Builder 4. Let’s see how this is making developers productive. We will use DCD to consume the data from the PHP file.

Creating a service

  1. Select the Data/Services window shown in the image above. If this is not visible, select it from Window -> Data/Services
  2. Click on “Connect to Data/Service” (highlighted in the image) in the Data/Services window
  3. Window as shown in the image below will be launched

Selecting service type

Since we want to communicate with the PHP we created, select PHP and click on next button.

Selecting PHP file for the service

In this screen:

  1. Browse and select the PHP class file to use. Make sure the PHP file is deployed in your web application
  2. Click on next button to continue

You will see a window displayed, saying Zend AMF library will be installed as shown in the image below. Just click on OK and continue will the set up. Flash Builder will deploy the Zend AMF on your PHP server.

Once the installation completes, Flash Builder will display a message. Clicking OK will display the functions in the PHP class. PHP class functions are referred as “operation”. Image below shows the window with operations available.

Click on finish to continue. Service created above can be seen in the services explorer as shown in the image below.

Configuring return type

In this step we will test the operation and configure the return type on the client i.e. we will specify what type of object to create with the response from the server. This will make it easy for us to develop, since it is easier to deal with strong typed objects.

Right click on the “getAllCustomers” operation in the services explorer and select “Configure return type” as shown in the image below.

A window as shown in the image below will be launched with options to configure the return type. You can chose an existing data type or let the Flash Builder generate VO classes based on the response from the server. Let’s leave it to the Flash Builder to generate required VO classes based on the server response as shown in the image below.

Click next to continue. You can see the response from the server in the window as shown in the image below.

Flash Builder introspected the Customer object returned from the server. Just click on finish. You can see the return type of the operation changed as shown in the image below. When we invoke the “getAllCustomers” operation, response will be object of the type “Customer”, in our case ArrayCollection containing Customer objects.

Displaying or binding service results in UI controls

We usually write code to display the response from the service in a DataGrid or any other control. Flash Builder 4 has an awesome option which allows you to just bind a service response to a control.

In this screen:

  1. Switch to design view
  2. Change the Application layout to “spark.layouts.VerticalLayout” using the properties panel
  3. Drag and drop a “DataGrid” from the “components” panel on to the design area
  4. Set the width and height properties of the “DataGrid” to 100%

Right click on the DataGrid and select “Bind to Data …” as shown in the image below.

A window as shown in the image below will be launched.

In this screen:

  1. We select a “New service call” because there are no existing services in the current application.
  2. Select the “CustomerService”
  3. Select “getAllCustomers():Customer[]” from the operations list
  4. Select “Customer[]” as Data provider
  5. Click OK

You can see the DataGrid in the design updated with properties of the Customer object as shown in the image below.

Save and run the application, you can see the data retrieved from the Remoting service and displayed in the DataGrid as shown in the image below.

More article on using Flash Builder 4 http://sujitreddyg.wordpress.com/flash-builder-4/

That’s it we created a Flex UI for an existing PHP class using DCD features in Flash Builder 4 in just few minutes. Flash Builder 4 rocks :)


Follow

Get every new post delivered to your Inbox.

Join 108 other followers