Legal
The views expressed in this blog are my own and do not necessarily reflect the views of Adobe Systems Incorporated.
Search
August 15, 2006
Running a Flex application locally and remotely
One of the problems working with Flex and a PHP backend is that the HTTP Service calls and RemoteObject calls need a complete path to the server. They don't accept relative URLs, only absolute URLs. This becomes a problem if you try and move things from a local testing server to a remote server. The URLs can get messed up, and it becomes painful to have to recompile a .swf file just to update a server name.
Here's a pattern that should help you as you create Flex applications and move them from a local server to a testing server to a production server. With this code, you don't need to modify the MXML file every time you move the resulting SWF file. Note: The code assumes that the default is http://localhost and will change to the server if it is being requested by something other than a file:// URL (this allows local testing to still work, again assuming that when you test locally, you're hitting the localhost server.)
Add the following code to your MXML file:
[Bindable]
public var hostname:String = "http://localhost";
public function onInit():void
{
var protocol:String = ExternalInterface.call("window.location.protocol.toString");
if( protocol != "file:" )
hostname = protocol + "//"+ ExternalInterface.call("window.location.hostname.toString");
}
Then, use something like this in ActionScript:
gateway = new RemotingConnection(hostname+"/adobe_php_sdk/libraries/amfphp/gateway.php");
or something like this in MXML:
<mx:HTTPService id="userRequest" url="{hostname}/adobe_php_sdk/samples/flex/simple_flex_php/request.php" useProxy="false" method="POST">
<mx:request xmlns="">
<username>{username.text}</username><emailaddress>{emailaddress.text}</emailaddress>
</mx:request>
</mx:HTTPService>
Note: This functionality is now implemented in the Adobe PHP SDK. You can see this by updating your code from SVN. The next release (.zip file) will have this implemented as well.
Comments
Hey Mike,
cool approach. But I never had Problems using relative URLs in my HTTP Services. Most of my services look like this:
url="php/serverconfig.php"
Ok, I have to say, that I've got my Flex Projects in my Webroot and configured the Output Folder URL (under Buildpath) of each projects that it's pointing to my Webroot as well. e.g.:
http://localhost/flex/CaptionProducer/bin/
Works fine for me, and I've got no problems moving my projects to another server. :)
I am trying to connect mysql with flex 2 and there is no way. You could send a complete code to me that works correctly. The one that it appears in your page to not works me, it appears the error:
1119: Access of possibly undefined property result through to reference with static type mx.rpc.http.mxml: HTTPService. main.mxml
Thanks.
I am trying to connect mysql with flex 2 and there is no way. You could send a complete code to me that works correctly. The one that it appears in your page to not works me, it appears the error:
1119: Access of possibly undefined property result through to reference with static type mx.rpc.http.mxml: HTTPService. main.mxml
Thanks.
Hi,
I am trying to connect mysql with flex 2 and there is no way too.
I have been trying for hours without success, could you send me please a code that works correctly.
Thanks a lot.
Thank you for signing in, You may now comment. (Sign Out)
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)