Note: Content on this page first appeared in the author's blog on October 23, 2008.
What? PHP, Zend, and Adobe goodness. Why? Because I'm obsessively compulsive and can't stop myself from iterating over a problem every which way possible. Depending on how you count it, there's somewhere between 13 and 54 different Flash, and/or Flex examples included in the archive - that doesn't even count the different PHP examples (also included). I started these examples in support of the Adobe and Zend announcements related to Flex Builder and AMF. With some help from Wade Arnold along the way, it's now a rather overwhelming collection.
I know who you are! You're that guy that uses Flex Builder, but not the Flex framework. It's true! It happens! I'll avoid the political mire that is editors in the Flash world, and just say that this example shows you how to connect to a "HelloWorld" PHP object from a pure ActionScript project developed with Flex Builder, but without the use of the Flex framework via Zend AMF.
It became plenty obvious to me early on that charting solutions in the PHP world are lame. Busting out bitmaps on demand? Calculating dimensions, drawing lines, figuring out ranges? Whatever! These examples walk you through building really effective and powerful charts using the Flex Charting components. It starts simply enough with getting some data into a LineChart. Then we add data tips. Then we add styles. Then we add formatting. Then we add filtering. Then we add alternating views via a ViewStack. Then we pimp it out and bring the full Zend Framework web site look and feel into a fully interactive data visualization application.
You know, because what's a Flex application without some data? I developed all these samples on MAMP, which includes MySQL. If you're using the same port information and default user name and password for your development/testing environment, then you'll be good to go. If you're not, and you want to just drop the data into your own configuration, I've provided you with the example data in XML format, as well as a SQL dump that includes the table schema.
It can seem counter intuitive at times, I know. Didn't we move to the web to get away from the desktop? Regardless of the shift, there are still good reasons that the desktop matters to web developers, and when you want to go there, you can do so easily, and cross-platform with Adobe AIR. These examples, take a Flex-based application, that connects to a PHP data access object (DAO) via Zend AMF to the desktop with Adobe AIR. You'll start with a basic web version, just shoved into an AIR window. Then you'll add native drag and drop to upload a vCard file. Then you'll check to see if the network is even available. Then you'll just work offline entirely using the embedded SQLite database that's part of AIR. Sample vCards provided!
Nobody likes code that's hard to maintain. So what's the first thing you'll do to take that old-school procedural code and make it maintainable? That's right, you'll pull all the core logic out to a separate PHP file and then include that file wherever you need that functionality. That's what this example shows. On the server, in PHP-land, all the core database access is put aside in it's own comfy script. That script is then included in numerous additional PHP files that expose functionality as XML. To help you get along, there's incremental builds for every step of the flex application - from getting that initial set of data to a complete CRUD application.
Did you know that the Flash authoring environment has included components for a long time? They're of a different nature than the Flex components, but they are there; great for one-off application style functionality in any otherwise immersive experience you might be creating. Presented here are two complete applications that use those components, and the supporting FLA files for Flash CS3 and Flash CS4. One of the applications connects to PHP via XML, while the other via Zend AMF. I make heavy use of the "document class" concept, so prepare yourself for ActionScript 3 goodness.
What could be better than good old fashioned "hello world?" The "ActionScript" example goes at Zend AMF using a pure ActionScript project. This example uses the Flex framework to connect to that "HelloWorld" PHP class Zend AMF. Sometimes you just need the basics to get you going, and if all the staged builds and various approaches have you down, this is the place to start.
You don't see it done too often, but there's no reason that our service calls from Flex have to be XML, SOAP or AMF. If you'd prefer to just get a bunch of text back, and process that data yourself, then be our guest. This example shows connecting Flex to a PHP endpoint that returns a plain text string. In this example, that value is just dumped to the screen, but you could use this approach to deal with comma-separated values (CSV) or any other text that might be provided by a service endpoint you don't control.
This is the de facto standard when it comes to services and Flash. You send a query string out with your HTTP request, and you get XML data back. Flash Player of course has really good XML manipulation via ECMAScript for XML (E4X). In this example, you'll pass that query string to a PHP resource, that will process it and return some data in XML. Quick, two guesses, what the response will say!
You weren't always the magic code man that you are today. You had to start somewhere. Or perhaps you're maintaining code that was somebody else's starting point. The most common way to get going before you understand the value of modularity and code reuse is to cram everything into one PHP file and just get the job done. This example covers that situation. If you're inclined to shove everything into one PHP script, this example builds out a complete CRUD application using XML as a return format to satisfy every brute force craving you might have.
Yes! It's true! PHP can be object oriented! I still hear biases that PHP is procedural, and this example sets out to prove that wrong. On the PHP side you'll get patterned to death by creating a DAO that uses a Factory (helper) to connect to a database, and then uses value objects (VO) in place of the traditional associative arrays. That well constructed object oriented infrastructure is then used to return XML to a Flex client. On the client side, once again, staged builds of a complete CRUD application are provided to gently walk you along learning Flex.
AMF in general likes objects, and Zend AMF is no exception. In the "Object" example, there's an additional PHP script for each CRUD operation. That means more work for you to expose you're wonderfully OO code as dumb XML. XML in it's own right is just downright heavy too - especially as it applies to the enterprise. In this example we'll take the classes from the "Object" example and expose them via Zend AMF to a Flex application. Just because I can, staged builds are included here too, to walk you along building a Flex application with complete CRUD operations.
You say "tomato" and I say "tomato" … Er, something like that. The Zend Framework includes REST server functionality. While it's not true REST, and uses the old query string in, XML out approach, we're still talking XML, which is what we want when we're building Flex applications. There are two examples here that use the Zend REST server functionality. Leveraging the externalized script from the "External" example, we can immediately expose that functionality as XML. You get one example that does that, but the problem is that the result isn't in a format that's especially friendly to iteration. Then there's another example that shows customizing the XML response from the Zend REST server to be more palatable to an SOA application. And yes, staged builds for complete CRUD functionality are provided here too.
<?xml version="1.0" encoding="UTF-8"?> <getContacts generator="zend" version="1.0"> <key_0> <id>1</id> <firstname>Ryan</firstname> <lastname>Stewart</lastname> <email>firstname.lastname@example.org</email> <mobile>123-456-7890</mobile> </key_0> <key_1> <id>2</id> <firstname>Mike</firstname> <lastname>Downey</lastname> <email>email@example.com</email> <mobile>111-222-3333</mobile> </key_1> <status>success</status> </getContacts>
Flash Player has included the ability to upload and download files for a while. The great thing about this functionality is that it allows you to specify a single or multiple file mode, and also lets you filter out files that you don't want the user to select. While the upload/download is going, you also get progress reports. It's among the most common uses for Flash, and as such here's an example of how to use the upload functionality from a Flex application to a PHP endpoint. This example expects you to upload a vCard (samples provided) to PHP, which then parses the data, puts a record in the database, and returns the details about the new record back to the client for display.
The problem with examples like these is that there's just never enough time to iterate over the code in every possible way (despite my obsession). Still on my list include using the Zend Framework MVC component to provide alternative presentations for the same logic, as well as real-time support using a custom socket server (written in PHP as well of course). I see a little chat and collaboration in my future. I'll make another post to update the code when I get there. In the meantime, if there's an example of functionality you'd like to see, then leave a comment a let me know!