Flash Builder 4.5 for PHP
For more information on Flash Builder 4.5 for PHP, see Using Flash Builder 4.5 for PHP, the Zend Studio User Guide or Using Adobe Flash Builder 4.5.
In this tutorial, you are going to create a Flex and PHP mobile application that retrieves, displays, and modifies database records (see Figure 1). A Flex application does not connect directly to a remote database. Instead, you connect it to a data service written in PHP. You will build the front-end Flex mobile application, the database and a PHP class, which is the server-side code used to manipulate database records. You will also run a PHPUnit Test to run and test the methods in your PHP Service to ensure they work properly.
In this module, you are going to build a Flex mobile application that retrieves data from a database and displays it. A list of employees will be displayed in one application view and employee details in another. First, you build the user interface, and then test the application using a desktop emulator.

Figure 1: The completed application.
Install Flash Builder 4.5 for PHP - For more information see the Flash Builder 4.5 for PHP Installation Guide.
Zend Server
5.x or above, or a compatible PHP web application server.
You will need a PHP application server to debug your application. Go
to http://www.zend.com/en/downloads/ to downlaod
Zend Server 5.x, the Zend PHP application server. For more information
see the Zend
Server Installation Guide.
The Zend Server
Installer includes optional components that can be downloaded as a
part of the installation process.
The two components you will need for this tutorial are:
MySQL
phpMyAdmin
Note:
Make sure to save the username and password for your Zend Server. If you installed the optional components MySQL and phpMyAdmin, the credentials are by default “root” as the username with no password.
Important Note:
If you already completed Build a Web (PHP) Application, you do not have to repeat this step. The database you created in that tutorial is identical to the database you will be creating here.
phpMyAdmin is a component that comes bundled with Zend Server in order to handle the administration of your MySQL databases. For more information see Working with phpMyAdmin to Manage MySQL in the Zend Server online documentation. Creating a database in MySQL is necessary in order to debug your Flex/PHP application. The PHP code in your project will use the database to access the information when needed, as defined by the code.
|
|
|
Figure 2: Open phpMyAdmin via Zend Server's Dashboard tab
Figure 3: Import tab in phpMyAdmin GUI
Figure 4: Import tab
|
To see the tables located in your database, select the database from the left menu of the phpMyAdmin UI and go to the Structure tab. |
|
In Flash Builder 4.5 for PHP, use the New Flex Mobile and PHP Project wizard to create a PHP project named PHPMobileService and a Flex project named MobileGettingStarted. Store the project files locally (see Figure 6) and use the default Mobile Settings (see Figure 5).
|
|
|
Figure 5: Create PHP Project dialog
Figure 6: Create Flex Project dialog
Figure 7: Specify mobile settings.
Figure 8: Assign a unique application ID. What You CreatedFlexWhen you create a new Flex Mobile project, Flash Builder 4.5 for PHP creates three files: two MXML files and one XML file (see Figure 9). You create Flex applications using two languages: ActionScript and MXML. Typically, you use MXML and Flex components to lay out application interfaces and ActionScript to program application logic. MXML tags and ActionScript code can reference each other, similar to HTML tags and JavaScript code. When you compile an application, the MXML is parsed and converted to ActionScript in memory, and then the ActionScript is compiled into bytecode. The result, a SWF file, is rendered by Flash Player for web applications and the Adobe AIR runtime for mobile and desktop applications. Flex mobile applications require the Adobe AIR 2.5 runtime which is available in the Android Marketplace.
Figure 9:View the MobileTestDrive project files in the Package Explorer view. MobileGettingStarted.mxml is the main application file. The first line of code is the XML declaration for the parser. The next line is the MobileApplication tag, which is the root container for the Flex mobile application. It has a firstView attribute set to views.MobileGettingStartedHomeView (the second MXML file automatically generated). This is the view that displays on the device when you launch the application. Unlike desktop or web applications built in Flex, you typically do not put much content in the application container. For mobile applications, most content is placed in individual views.
The third file created, MobileGettingStarted-app.xml, is the application descriptor file that is used by the AIR runtime when installing and launching the application. It includes info about the application (including its name, id, associated icons, what the initial window should look like and more). PHPWhen you create a PHP project Flash Builder 4.5 for PHP creates a container where you can create and manage all your PHP resources for your Flex Mobile/PHP application. Your PHP project is created with Zend Framework already built in, along with a services, public and library folder. Your services folder is where you will create and manage all the PHP Services you will use in your application. A PHP Service is a PHP class containing PHP methods. The PHP methods you define in your PHP Service are used as operations for the Flex side of your application. The library folder will contain the business logic of your application. The public folder holds all the public resources of your application, which are all the resources that will be visible to the client. Also included in the public folder is the Gateway script. The gateway is responsible for converting objects and translating the results from PHP methods into native ActionScript objects used in the Flex User Interface. The Zend Framework library is included in the Include Path of your PHP project automatically. You can see this and the resources contained within it by opening the PHP Explorer view (see figure 10).
Figure 10: PHP Explorer view |
|
|
|
|
|
The gateway script initializes the Zend AMF Server, which is able to translate between Flex/ActionScript and PHP, and the Zend Framework Autoloader which enables dynamic class loading. For more information see The Gateway Script. |
|
|
|
|
|
Add
the assets folder (available in the Building_A_PHP_App.zip
file) under the src folder (see Figure 11).
Figure 11: Add the assets folder to your project. |
|
|
|
|
|
Figure 12: Add a TextInput to the title area of the ActionBar container.
Figure 13:The parts of the ActionBar container.
Figure 14: Use the Properties view to set object properties, styles, and more.
|
|
|
|
|
|
Figure 15: Add Buttons to the action area of the ActionBar container.
Figure 16: Specify multi-resolution bitmaps.
|
|
|
|
|
|
Figure 17: Use the Spacer components to finetune the layout. |
|
|
|
|
|
Figure 18: Set the size and position of a component using constraints.
|
|
|
|
|
|
Figure 19: Use Content Assist to see a list of all possible tag attributes. |
|
|
|
|
|
In your Library folder, create two PHP file's by going to File | New | PHP File and name them Employee and Department. Copy the Employee.php and Department.php code into the files and save the project.
Figure 20: The PHP Explorer view showing your PHP Service and the Department and Employee files. |
|
|
|
|
|
Figure 21: Select Class from the Right Click Menu of your services folder.
Figure 22: The Create New PHP Class dialog.
|
|
|
Running a PHPUnit test will allow you to test the methods in your
PHP service, ensuring that individual methods are working properly. In
the same way that you can debug an application to diagnose problems within
it, you can run a PHPUnit test to help you diagnose any problems or errors
in your PHP class. Testing an individual method is one option (see figure
28), but running a PHPUnit Test will allow you to test all of the methods
in the class simultaneously.
In this case, we are using this resource to ensure that our PHP class is
working smoothly to prevent any future errors.
|
|
|
Figure 23: Select PHPUnit Test Case from the New wizard.
Figure 24 You can see the new test file created for your PHPUnit Test in the PHP Explorer view.
Figure 25: Run the file as a PHPUnit Test to run and test the methods in your PHP Service.
Figure 26: A successful PHPUnit Test with the lines covered in the Code Coverage tab. |
|
|
After you have created your PHP services, you need to connect it to your Flex project. This makes the methods in the PHP service easily accessible for binding while developing the Flex side of your application. Once you have introspected your PHP service, you can use the methods contained within it as operations for the Flex component's in your applications GUI.
Introspecting PHP services in Flash Builder 4.5 for PHP can be done in two ways; using the Right Click Menu or using the Data/Services view. Here we will go through the process using the Right Click Menu.
|
|
|
Figure 27: See all the public methods included in your PHP service in the Data/Services view.
Figure 28: The Test Operation view lists all of the names and values received from the specific method you tested.
Figure 29: Use the drag and drop functionality to drag a method directly into the UI.
|
|
|
|
|
|
Figure 30: View a component's API. |
|
|
|
|
|
Figure 31: Create a run configuration to emulate the application on a specific mobile device.
Figure 32: Preview the application in the AIR Debug Launcher.
Note: If you do not see the ADL menu, click on Flash Builder and then return to your application and click it. You should now see the ADL menu.
Figure 33: Use the ADL Device menu to simulate device actions.
Figure 34: View device configurations.
|
|
|
|
|
|
Figure 35: When debugging your Flex Mobile and PHP application, you will see the results in the Debug perspective. |
|
|