Zend Framework and AMF
Zend Framework (ZF) has a handy component called Zend_Amf which provides a means for your PHP to communicate with Adobe's Action Message Format (AMF).
Serving AMF data from ZF is plain easy, it's also very simple to integrate into your existing website. As ZF is a component based framework you could simply pick out the Zend_Amf and related classes and use them. But obviously the integration is better if you have a full ZF stack.
For renault.tv we had setup an AMF API. Since we wanted to serve both HTML and AMF from the same backend we opted to setup the AMF server as a controller under ApiController.php:
We started with Matthew Weier O'Phinney's pastebin application which we found to be an excellent starting point for the structure of our boostrap and initialization code.
During development we quickly found the AMF response times to be slow, especially as the data size returned grew larger. The following .htaccess conditions helped improve performance drastically:
Here are a few best practices we picked up during development:
Go crazy!
Serving AMF data from ZF is plain easy, it's also very simple to integrate into your existing website. As ZF is a component based framework you could simply pick out the Zend_Amf and related classes and use them. But obviously the integration is better if you have a full ZF stack.
For renault.tv we had setup an AMF API. Since we wanted to serve both HTML and AMF from the same backend we opted to setup the AMF server as a controller under ApiController.php:
class ApiController extends Zend_Controller_Action
{
public function preDispatch()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
}
public function indexAction()
{
// instantiate server
$server = new Zend_Amf_Server();
// set production mode to true to suppress debug messages
$server->setProduction(false);
// handle request
$response = $server->handle();
echo($response);
}
}
We started with Matthew Weier O'Phinney's pastebin application which we found to be an excellent starting point for the structure of our boostrap and initialization code.
During development we quickly found the AMF response times to be slow, especially as the data size returned grew larger. The following .htaccess conditions helped improve performance drastically:
# Set some default PHP values
php_flag zlib.output_compression 1
php_value zlib.output_compression_level 2
# Gzip CSS, JS and AMF
AddOutputFilterByType DEFLATE text/css application/x-javascript application/x-amf
Here are a few best practices we picked up during development:
- All API functions receive an object pass back an object - this proved to be a very extensible approach and seemed to work well with Flash.
- Unit test all code. We setup unit tests for both PHP and Flash. As you may soon realise debugging AMF is very hazardous. To help easy the pain we use jQuery's qUnit test suite to mirror AMF calls issued from Flash. More on this in a future post.
- Use a HTTP Proxy to inspect your AMF output. Charles is highly recommended if you're on a Mac.
- Make sure no trailing spaces are left in your code output - editors tend to do this a lot. A good way to avoid this is to not close your PHP tags.
- We found associative arrays very problematic as the keys get lost in translation. So try to avoid them. Passing back objects is a good way to avoid this issue.
Go crazy!
Labels: amf, php, technical, zendframework

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home