Invoking PHP objects with <mx:RemoteObject>
Search:
 

MINI TUTORIAL

Invocation of a PHP object using the <mx:RemoteObject> MXML declaration can be done in just a few simple steps. This mini tutorial will guide through the setup and configuration:
  1. Download and install WebORB for PHP (use the Getting Started guide if you need assistance with this step).
  2. Deploy the PHP class you will be invoking from Flex into /app/services folder of your Rails application.
  3. Open /Weborb/WEB-INF/flex/remoting-config.xml in a text editor and add a destination declaring your PHP class using the following format:
  4. <destination id="destination-name">
       <properties>
         <source>Your.PHP.ClassName</source> 
       </properties>
    </destination>
    
    where,
    • destination-name is an id assigned to your PHP class. The id will be used by Flex client when invoking server-side methods
    • Your.PHP.ClassName is the full name of the PHP class exposed as a service to Flex clients
       
  5. Create a Flex Builder project as it is described in the "GETTING STARTED - CREATING FLEX APPLICATION" section in this article.
  6. Add the following remote object declaration to your MXML's application markup:
    <mx:RemoteObject id="remote-object-id" destination="destination-name"
                     showbusycursor="true" fault="faultHandler(event)">
        <mx:method name="methodToInvoke" result="successHandler(event)">
    </mx:RemoteObject>
    
    where,
    • destination-name must be the same literal as destination id from remoting-config.xml
    • remote-object-id is an id used in the MXML application to refer to a remote object
    • methodToInvoke is the method name available in the remote destination that your remote object can invoke
    • successHandler and faultHandler are function references your MXML application will invoke upon successful or unsuccessful method invocation
       
  7. Suppose the following PHP class needs to be consumed by a Flex client:
    class MyPHPHelloWorld
    {
      public function sayWorld()
      {
         return "Hey Flex";
      }
    }
    
    then the destination declaration may look as the following:
    <destination id="helloWorldDestination">
      <properties>
         <source>MyPHPHelloWorld</source>
      </properties>
    </destination>
    The <RemoteObject> declaration would have the following contents:
    <mx:RemoteObject id="helloWorldService" destination="helloWorldDestination"
                     showbusycursor="true" fault="faultHandler(event)">
        <mx:method name="sayHello"result="gotHello(event)">
    </mx:RemoteObject>
    The callback handler functions (gotHello and faultHandler) could be declared as:

    private
    function gotHello(event:ResultEvent):void
    {
       Alert.show("Server said: " + event.result, "Success");
    }

    private function faultHandler(event:FaultEvent):void
    {
       Alert.show(event.fault.faultstring, "Error");
    }

     
  8. Now anywhere in your MXML application you can invoke sayHello using the following line of code: helloWorldService.sayHello();
     
  9. Locate the directory where Flex Builder places the compiled client application. It is very important to place a copy of weborb.php into the same directory. weborb.php must contain the following PHP code:

    <?php 
       require_once("../Weborb/ORBHttpHandler.php");

       $m_ORBHttpHandler = new ORBHttpHandler();
       $m_ORBHttpHandler->processRequest();
    ?>

    The first line must be edited to point to the Weborb directory from the product distribution. For example, look at weborb.php located in the /Console folder.

 
Copyright © 2003-2007 Midnight Coders, LLC.  Privacy Policy | Contact Webmaster
Home | Products | Customers | Download | Licensing | Forum | About