Building Your First PHP-Based Web Application
In this tutorial, you are going to create a Flex and PHP web application that retrieves, displays, and modifies database records (see figure 1). A Flex and PHP application does not connect directly to a remote database, but instead,you connect it to a data service written in PHP. You will build the front-end Flex application, the database and a PHP class, which is the server-side code used to manipulate database records. Then you will run a PHPUnit Test to run and test the methods in your PHP Service to ensure they work properly. And finally, you will run the application.

Figure 1: The completed  application
In this module, you are going to build a Flex and PHP project (a Flex/PHP application) that retrieves data from the PHP database and displays it. Employee data will be displayed on one "page" in the application and Department data on another.
Prerequisites
- 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.
- Download the Building_A_PHP_App.zip file and put the files somewhere accessible. These are all the resources you will need to complete this tutorial.
 
Step 1: Create a MySQL Databse Using phpMyAdmin
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. 
 | |
Step 2: Create a New Flex and PHP Project
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 5) and use the default Mobile Settings (see Figure 6).
 ![]() 
Figure 5: Create a new PHP project called PHPWebService 
 ![]() 
Figure 6: Create Flex project 
 
 What You CreatedFlex
When you create a new Flex/PHP project, Flash Builder 4.5 for PHP creates an MXML file under the Flex project with the same name as the project (see Figure 7). This is the main application file where you add your client-side code. You create the client side of Flex applications using two languages: ActionScript and MXML. Typically, you use MXML and Flex components to create application interfaces and ActionScript and events to program application logic. MXML tags and ActionScript code can reference each other, similar to HTML tags and JavaScript code. You use PHP to create the server-side code. 
When you compile an application, a SWF file is created. You reference the SWF file in an HTML page and the SWF file is downloaded and rendered by Flash Player (available as a browser plugin or ActiveX control). 
![]() 
Figure 7: Create the TestDrive project 
In WebGettingStarted.mxml, the first line of code is the XML declaration for the parser. The next line is the <s:Application> tag, which defines the Application container that must be the root tag for a Flex application. When the application is compiled, a SWF file, an HTML wrapper page that references the SWF file, and other files are placed in the bin-debug folder on your application server so you can browse to the application. 
PHP
When 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/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 8). 
![]() 
Figure 8: PHP Explorer view 
 | |
Did you know...
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. 
 | |
Step 3: Use Design Mode to Add Components and Set Properties
 ![]() 
Figure 9: Arrange components as shown here 
 
<s:Label x="36" y="36" text="XYZ Corporation Directory" color="maroon" fontSize="20"/> 
  | |
Step 4: Change Component Attributes in MXML
Use the Flash Builder Content Assist to select and set values for various properties and styles.
 ![]() 
Figure 10: Use Content Assist to see a list of all possible tag attributes 
 ![]() 
Figure 11: View a component's API 
 | |
Step 5: Create Your PHP Project Resources
In your Library folder, create two PHP file's by going to File | New | PHP File and name then Employee and Department. Copy the Employee.php and Department.php code into the files and save the project. 
![]() 
Figure 12: The PHP Explorer view showing your PHP Service and the Department and Employee files. 
 | |
Step 6: Create Your PHP Service
 ![]() 
Figure 13: Select Class from the Right Click Menu of your services folder. 
![]() 
Figure 14 The Create New PHP Class dialog. 
 
  | |
Step 7: Run a PHPUnit Test on Your PHP Service
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 20), 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.
In this case, we are using this resource to ensure that our PHP class is working smoothly to prevent any future errors.
 ![]() 
Figure 15: Select PHPUnit Test Case from the New wizard. 
 ![]() 
Figure 16: You can see the new test file created for your PHPUnit Test in the PHP Explorer view. 
 ![]() 
Figure 17: Run the file as a PHPUnit Test to run and test the methods in your PHP Service. 
The PHPUnit view opens with the results of the test. For a successful test you will see a green bar and the covered lines in the Code Coverage tab (see figure 18). For a failed test, there is a red bar with a list in the Trace failure tab of the specific lines in the code that failed. ![]() 
Figure 18: A successful PHPUnit Test with the lines covered in the Code Coverage tab. 
 | |
Step 8: Introspect and Bind
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 19: See all the public methods included in your PHP service in the Data/Services view. 
 ![]() 
Figure 20: The Test Operation view lists all of the names and values received from the specific method you tested. 
 ![]() 
Figure 21: Use the drag and drop functionality to drag a method directly into the UI. 
 | |
Step 9: Run the Application
 ![]() 
Figure 22: View the application in a browser 
 | |
Step 10: Debug Your Application
 ![]() 
Figure 23: When debugging your Flex Mobile and PHP application, you will see the results in the Debug perspective. 
 | 





















No comments:
Post a Comment