Zend_AMF and Flex

Many people are switching from old school php type architectures to a more object oriented MVC system to pass data back and forth between front ends and back ends. In the world of php and flex, this generally means either Zend_AMF or AMFPHP.

If you want to see how this works, I suggest starting here.

http://corlan.org/2008/11/13/flex-and-php-remoting-with-zend-amf/

It’s a snap to do and you can probably get your first simple test up and running in a single day.

Here’s what the tutorial doesn’t say that maybe it should.

1. Start by creating a pop up title window to display your AMF errors with and make it really useful. Stick your ‘remoteFault’ method into your singleton so you can use it everywhere. I have a little MXML called RPCErrorWindow that’s basically just a TextArea that I send data into.

public function remoteFault(event:FaultEvent, caller:DisplayObject):void {
pop = mx.managers.PopUpManager.createPopUp(caller, RPCErrorWindow,true) as RPCErrorWindow;
pop.rpcErrorString = event.fault.message;
pop.showCloseButton = true;
pop.width=500;
pop.height=300;
pop.title=”RPC Error Console”;
pop.addEventListener(CloseEvent.CLOSE, closeRemoteFaultViewer);
PopUpManager.centerPopUp(pop);

}

2. In the services-config.xml file, you point to a service endpoint. Make sure to set production false so the errors display in the title window above.

$server = new Zend_Amf_Server();
$server->setProduction(false);

3. In the same file above, you don’t need to get fancy to add multiple ‘service’ files or multiple VO objects. Just add them in.

//To add multiple services…

$server->addDirectory(dirname(__FILE__) .’/../service/’);

//To add multiple VO objects.

$server->setClassMap(“OrderVO”, “OrderVO”);
$server->setClassMap(“AddressesVO”, “AddressesVO”);

4. In your services, make them extend something like a generic service class and in that class, have a print function that sends everything to the phperrors.log file. You’ll get really tired of writing error_log(print_r($object, true)). Here’s my generic service method that I just call with parent::p($myObject);

public function p($object) {
error_log(“Dumping variables from object of type ” . get_class($object));
error_log(print_r($object, true));
}

5. And lastly, just because you’re in an instantiated service class, that doesn’t mean you can daisy chain functions like you’re probably used to in PHP.  In my ‘address service’ class, I have methods that deal with getting address objects, and I have two little wrapper functions I call from the flex. Notice below I call AddressService::getAddress instead of just this.getAddress or even getAddress.

public function getBillingAddress($user){
$address = AddressService::getAddress($user, “billing”);
return $address;
}
public function getShippingAddress($user){
$address= AddressService::getAddress($user, “shipping”);
return $address;
}

That’s about it. Overall, it’s much easier than you’d expect.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

You must be logged in to post a comment.