Saturday, May 16, 2009

Sending and receiving VOs in AMFPHP #1

Last week, I have worked to integrate sending Value Objects via AMFPHP instead of simple arrays in our current project and I would like share my knowledge for people who may have trouble getting started as I have.

I will detail the steps over three posts:

  1. Creating VOs on PHP and AS3

  2. Sending and Receiving VOs through AMFPHP

  3. Sending nested objects in AMFPHP

The first thing you need to do is edit globals.php on the server and edit the $voPath to the correct path for amfphp to find the vo files it receives to map against:

globals.php

$voPath = "path/to/your/vo/";

Then create the actual VO class in that path. Let's say the file is in "path/to/your/vo/users/UserVO.php". Because the vo path is set to "path/to/your/vo", the $_explicitType of the class should be "users.UserVO" because the file is located in "users/UserVO.php" relative to the vo path. This is important because this is the path the server will check for the corresponding VO file to map when it receives this VO from the Flex client.

UserVO.php
<?php
class UserVO
{
public $firstName;
public $lastName;
public $email;

public $_explicitType="users.UserVO";
}
?>

We need to create an AS3 equivalent of the UserVO class on the Flex side, but this time each property is strongly typed to the expected object types:

UserVO.as
package com.flexbreakpoints.vos
{
[RemoteClass(alias="users.UserVO")]
[Bindable]
public class UserVO
{
public var firstName:String;
public var lastName:String;
public var email:String;
}
}

The RemoteClass metatag is the key to identifying the object Flex receives from AMFPHP. The alia string must be identical to the $_explicitType in the PHP class it mirrors. This string is what Flex uses to map the received object to the correct AS3 VO. If no match is found, the object will default to ObjectProxy.

Remember, both the $_explicitType and RemoteClass strings should always be the PHP VO's path relative to the $voPath and not the AS3 VO's package. When you are only receiving the VO the strings only need to MATCH, and not necessarily the PHP VO's path, but it is always good idea to in case you need to send it back to the server.

No comments:

Post a Comment