sfAmfPlugin
Overview
The sfAmfPlugin provides you with all you need to write symfony backends for Flex clients.
The communication is done via Adobes AMF protocol. On serverside the SabreAMF library is used
to encode/decode the AMF streams. For parsing the annotations the Addendum library use used
Installation
The easiest way to install the plugin is to use the plugin:install command of symfony.
$ php symfony plugin:install sfAmfPlugin
If you preffer you can use the current development version from the subversion repository
(http://svn.symfony-project.com/plugins/sfAmfPlugin) instead. Copy the checked out version to the plugins folder of
your project and execute the command
$ symfony cc
That's it
Usage
AMF-Client Requests are try to execute Services on the backend. Therefore you have to create a RemoteObject in
Flex that has a Symfony-URL (i.e http://flextest/Test/amf).
All your services you can call will need to be saved under lib/services in your project folder. There you can
add as many subfolders as you need/want. Services on Flex-Side can use package names like
org.company.project.Servicename. You can use this with symfony too. All you need to do is to save this
Service under lib/services/org/company/project/Servicename.php. That's it!
A service can look like this:
[php]
class TestService extends sfAmfService {
public function getValues() {
return array('value1', 'value2', 'value3');
}
}
Please keep in mind that your service needs to extend the sfAmfService class. By default every public function
is accessable via a AMF-Request (you can change this with annotations, see below).
Instead of creating the Services by hand, you can use the amf:create-service commandline task.
$ symfony amf:create-service [--package=...] service_name
Sample 1:
$ symfony amf:create-service User
will create the file UserService.class.php in the folder lib/services of your project
Sample 2:
$ symfony amf:create-service --package=de.shiftup.projectname User
will create the file UserService.class.php in the folder lib/services/de/shiftup/projectname of your project
But how is this service called from client side? We need an URL to do so. For symfony this means you need a
module and an action. The following listing shows you how an AMF action should look like:
public function executeAmf() {
$this->setLayout(false);
$gateway = new sfAmfGateway();
$response = sfContext::GetInstance()->getResponse();
$response->setContent($gateway->service());
return sfView::NONE;
}
ORM-Support
The sfAmfPlugin supports both ORM-Layers of Symfony (Propel and Doctrine). So you are able to return
Doctrine_Recordset, Doctrine_Collection and Propel-Objects from your service. The plugin is doing
the conversion to AMF for you.
Sample:
class TestService extends sfAmfService {
public function getValues() {
$result = Doctrine::getTable('Menu')->findAll();
return $result;
}
}
Annotations
You can control the behaviour of the Service via setting annotations in the DocBlock comments of the function.
AmfIgnore
By default every public funtion is accessable via a AMF-Request. With the @AmfIgnore Annotation you can set
a public function to inaccessable. If you try to access this function you get an error message.
/**
* @AmfIgnore
*/
public function getValues() {
...
}
AmfClassMapping
AMF has a nice feature called class mapping. In this case your result of the PHP will be automatically mapped
to a ActionScript class on the Flex client side.
Cause Flex can not do this without some informations you have to define the ActionScript class before returning
the values from PHP. With the sfAmfPlugin you can use the AmfClassMapping annotation to define this class name.
/**
* @AmfClassMapping(name="ActionScriptClassName")
*/
public function getValues() {
...
}
AmfReturnType
Besides class mapping the AMF plugin can convert your return data in some special ActionScript data types before
sending them back to the Flex-Client
ArrayCollection
If you want to transfer array data you can use the return type ArrayCollecion. If so the data will casted in a
ArrayCollection on Clientside automaticaly.
/**
* @AmfReturnType("ArrayCollection")
*/
public function getImage() {
$values = array();
$values[] = ...;
$values[] = ...;
return $values;
}
ByteArray
Sometimes it can be useful to transfer byte array data (i.e. images) from PHP to Flex. Therefore you can set the
returning data type of a service method to ByteArray. This is done via the @AmfReturnType annotation
/**
* @AmfReturnType("ByteArray")
*/
public function getImage() {
$image = file_get_content('path/to/image.png');
return $image;
}
TODO
- Annotations for authorization
- Added Configuration-File support for the service folder
- Adding factory class for AMF controller
I like to hear from you! Maybe you have an idea how to enhance the plugin. Just send me an email!
Changelog
- 1.2.1 (04-12-2009)
- Fixing bugs in Documentation
- Fixing installation problems over the plugin:install task
- 1.2.0 (04-09-2009)
- Added symfony 1.2 compatibility
- Updated SabreAMF to version 1.2.203
- Fixing problem with Service-Classes with package names
- Added amf:create-service commandline task for creating service files
- 1.1.0 (08-27-2008)
- Added support for ArrayCollections
- Added support for ByteArrays
- 1.0.0 (08-26-2008)
- Added support for the annotations @AmfIgnore and @AmfClassMapping
- Updated Documentation
- 0.9.4 (08-25-2008)
- Fixing bugs in package file
- 0.9.3 (08-25-2008)
- Adding support for Propel objects
- 0.9.2 ((08-24-2008)
- Fixing some smaller bugs
- PEAR packaging for symfony installer support
- 0.9.1 (08-21-2008)
- Adding support for Doctrine objects
- 0.9.0 (08-20-2008)
License
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.