PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Connecting to the Server from Flex

Connecting to your Zend_Amf_Server from your Flex project is quite simple; you simply need to point your endpoint URI to your Zend_Amf_Server script.

Say, for instance, you have created your server and placed it in the server.php file in your application root, and thus the URI is http://example.com/server.php. In this case, you would modify your services-config.xml file to set the channel endpoint uri attribute to this value.

If you have never created a service-config.xml file you can do so by opening your project in your Navigator window. Right click on the project name and select 'properties'. In the Project properties dialog go into 'Flex Build Path' menu, 'Library path' tab and be sure the 'rpc.swc' file is added to your projects path and Press Ok to close the window.

You will also need to tell the compiler to use the service-config.xml to find the RemoteObject endpoint. To do this open your project properties panel again by right clicking on the project folder from your Navigator and selecting properties. From the properties popup select 'Flex Compiler' and add the string: -services "services-config.xml". Press Apply then OK to return to update the option. What you have just done is told the Flex compiler to look to the services-config.xml file for runtime variables that will be used by the RemotingObject class.

We now need to tell Flex which services configuration file to use for connecting to our remote methods. For this reason create a new 'services-config.xml' file into your Flex project src folder. To do this right click on the project folder and select 'new' 'File' which will popup a new window. Select the project folder and then name the file 'services-config.xml' and press finish.

Flex has created the new services-config.xml and has it open. Use the following example text for your services-config.xml file. Make sure that you update your endpoint to match that of your testing server. Make sure you save the file.

<?xml version="1.0" encoding="UTF-8"?>
<services-config>
    <services>
        <service id="zend-service"
            class="flex.messaging.services.RemotingService"
            messageTypes="flex.messaging.messages.RemotingMessage">
            <destination id="zend">
                <channels>
                    <channel ref="zend-endpoint"/>
                </channels>
                <properties>
                    <source>*</source>
                </properties>
            </destination>
        </service>
    </services>
    <channels>
        <channel-definition id="zend-endpoint"
            class="mx.messaging.channels.AMFChannel">
            <endpoint uri="http://example.com/server.php"
                class="flex.messaging.endpoints.AMFEndpoint"/>
        </channel-definition>
    </channels>
</services-config>

There are two key points in the example. First, but last in the listing, we create an AMF channel, and specify the endpoint as the URL to our Zend_Amf_Server:

<channel-definition id="zend-endpoint"
    <endpoint uri="http://example.com/server.php"
        class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>

Notice that we've given this channel an identifier, "zend-endpoint". The example create a service destination that refers to this channel, assigning it an ID as well -- in this case "zend".

Within our Flex MXML files, we need to bind a RemoteObject to the service. In MXML, this might be done as follows:

<mx:RemoteObject id="myservice"
    fault="faultHandler(event)"
    showBusyCursor="true"
    destination="zend">

Here, we've defined a new remote object identified by "myservice" bound to the service destination "zend" we defined in the services-config.xml file. We then call methods on it in our ActionScript by simply calling "myservice.<method>". As an example:

myservice.hello("Wade");

When namespacing, you would use "myservice.<namespace>.<method>":

myservice.world.hello("Wade");

For more information on Flex RemoteObject invocation, visit the Adobe Flex 3 Help site.

Zend Framework