Archive for the 'Flex' Category

Flex and Drupal Paths

erich October 10th, 2008

At CommonPlaces, each developer has his or her own sandbox to code in. Each sandbox can run n instances of a Drupal application, which all run out of subdirectories from the developer’s web root. So, for example I have a structure like this:

  • /public_html
    • /drupal_site1
    • /drupal_site2

We have a similar staging environment for all of our QA, and then of course, we have the production servers.

When I develop Flex applications that need to connect to one of these instances, I run into fun with dynamic urls to the /services/amfphp gateway. Because Flex can interact with Javascript, it’s trivial to build dynamic urls to whatever environment the Flex app is running in.

Step 1: Download and enable the JSTools module for Drupal. This module extends the Drupal javascript object and gives it extra properties.

Step 2: Construct your urls dynamically in Flex by utilizing the ExternalInterface object.


var protocol:String = ExternalInterface.call(’location.protocol.toString’);
var domain:String = ExternalInterface.call(’location.hostname.toString’);
var urlPath:String = ExternalInterface.call(’eval’, ‘window.Drupal.settings.jstools.basePath’);
var gatewayUrl:String = protocol + “//” + domain + urlPath + “services/amfphp”;

Note that the protocol and domain strings are set via ExternalInterface calls to a javascript function. The urlPath string is set via a call to the eval() function, and I’m passing in an argument of the Drupal basepath property set by jstools.

Debugging this is also easy, if you use Firefox and the Firebug plugin. All you have to do is pass debug strings to the console:


ExternalInterface.call(’console.info’, ‘protocol = ‘ + protocol);
ExternalInterface.call(’console.info’, ‘domain = ‘ + domain);
ExternalInterface.call(’console.info’, ‘path = ‘ + urlPath);
ExternalInterface.call(’console.info’, ‘gateway url = ‘ + gatewayUrl);

With the dynamic url to your amfphp gateway set, you can be assured that your Flex client will be able to consume data from Drupal services, no matter where the Flex app is run.

tags: , , , , , , , , , ,