Tuesday, June 26, 2007

Flex,PHP, MYSQL example

http://blogs.adobe.com/mikepotter/2006/06/download_a_flex_1.html

Download a Flex / PHP Framework, with Source Code
Digg This!
When we were at the New York PHP Conference last week, one of the things that struck me was how hard it was to get started with Flex and PHP. You have to go to one site to download Flex Builder, then to another site to download AMFPHP, then build out your application, deploy it etc...

I thought I could help out, so I built a small framework that includes AMFPHP along with some sample files. The Flex application simply displays information from a MySQL database in a Flex application. Very simple. Here's how you, as a PHP developer, get started:

1. Download the Flex / PHP Framework .zip file.

2. Extract the .zip file in your website. The files must be put in a directory called flex_project in your web server's root directory (ie. /flex_project) (the .zip file should create the flex_project automatically, so you should just extract to the root dir.).

3. Create a database called sample, and populate it with the data from sample.mysql in the flex_project directory.

4. Modify the line $mysql = mysql_connect(...); in the file flex_project/amfphp/services/sample.php to be able to connect to your database.

5. Visit http://localhost/flex_project/amfphp/services/sample.php. (Change localhost if you haven't installed on a local machine). You should see some output.

6. Visit http://localhost/flex_project/bin/flex_project.html to see the Flex application in action. You will need Flash Player 9 for this to work properly.

To modify the backend PHP code, you'll want to change the file in flex_project/amfphp/services/sample.php

To modify the front end Flex code, you'll want to download Flex Builder, create a new Flex project with the source of it as the flex_project directory. Then, modify the flex_project/flex_project.mxml file. When you save that file, it will update the .swf file in the bin/ directory, so reloading http://localhost/flex_project/bin/flex_project.html should show you an updated application.

One thing to note that I've changed in this compared to other uses of Flex and AMFPHP. In AMFPHP, I commented out the following lines in amfphp/app/Filters.php:
$outHeader = new AMFHeader("AppendToGatewayUrl", false, "?" . ini_get('session.name') . "=" . $session_id);
$amf->addOutgoingHeader($outHeader);

I'm not too sure what that was doing, but it was calling an ActionScript function AppendToGatewayURL that was blank in my previous tutorials. Doing this removed the need to create a RemotingConnection.as file in your Flex Project. You'll notice this because the connection to gateway.php now looks like this:

gateway = new NetConnection();
gateway.objectEncoding = flash.net.ObjectEncoding.AMF0;
gateway.connect( "/flex_project/amfphp/gateway.php");
gateway.call( "sample.queryDatabase", new Responder( onResultQueryDatabase, onFault ) );

Hopefully this .zip file is helpful for PHP developers. It shouuld be relatively easy to extend this sample to include the ability to create, update and delete elements from the database. If you do that, please send me an updated .zip file to post on this site.

1 comment:

Unknown said...

Nice example about this PHP. Thanks for sharing .