Flex remoting with CakePHP - CpAmf plugin
A new version of the CpAmf plugin is available. Check out the new version here.
The goal of this project was to use CakePHP controllers as flash remoting services. The basic idea was to use AMFPHP 1.9, because we used it before and it was a good choice in earlier projects. As nobody wants to "reinvent the wheel", I googled for a solution to integrate the AMFPHP into the CakePHP framework.
After a short research I found the following solutions:
I tried both solutions, but cakeAMFPHP does not use the latest AMFPHP, and with cakeAMF there was a problem using flex RemoteObjects.
The idea for my implementation came from cakeAMFPHP.
This plugin is based on the latest AMFPHP package, and works with CakePHP 1.2. The amf plugin uses all features of the AMFPHP package: works with or without the AMF PHP extension (if amfext is installed and enabled, it will be automatically used by this plugin).
CpAmf plugin allows you to use CakePHP controllers as "services", using all CakePHP controller features (models, behaviors etc.), also works with flex RemotingObject, and can be used with MATE framework (flex).
Just copy the plugin into your cake application's "plugins" directory. To check the gateway installation, simply open the following url:
You should see a message like this:
amfphp and this gateway are installed correctly.
You may now connect to this gateway from Flash.
If you have the amf extension you shuld see this in message:
AMF C Extension is loaded and enabled.
Value Object mapping
Amfphp has a useful feature: the VO mapping. CpAmf plugin uses this feature in a bit specialized way. We create a model in CakePHP, and a Class in flex which corresponds to our model.
The metedata tag to achieve this mapping is:
In our model we create an afterFilter method, and use the cakePHP built in Set::Map() function to convert the associative array to an object (or array of objects). We use generic class here (php's dummy class 'stdClass'), set the _explicitType property of all objects, and unset the _name_ property (which is set by Set::Map() method), because we don't need this property in our flex class. This approach allows us to change the model, and the corresponding flex class without the need for changing the vo classes (we don't even need to create them).
When we get data (object) from flex we don't use mapping, on php side we use associative arrays. The amfphp vo directory is set to default value (vendors/amfphp/services/vo). You can change this value if you want to use objects on php side (vendors/amfphp/globals.php).
We use one special vo class: ArrayCollection.php, it allows us to map an array of objects (php side) to an ArrayCollection of objects (flex side). Thanks to Wade Arnold for this solution.
Using the service browser
AMFPHP comes with a handy utility called service browser, which is useful for testing the services (or in this case controllers). Although you can test your controllers with CakePHP itself, but if you want to test them using flash remoting you can use the service browser.
The browser is accessible at the following url:
NOTE: The service browser is working only when the debugging is enabled in cakePHP.
Special thanks to the developers of: