Integrating zend-amf into Yii Framework
This post explains how to integrate the Zend-AMF 1.11.10 into the Yii Framework in order to provide an AMF endpoint to your Flex/AS3 applications.
Requirements
Yii 1.1.8 Zend-AMF 1.11.10 Supposing that have you already followed the installation guide to install Yii and have a fresh app in some local webserver, you will need to extract the contents of the ZendAMF zip file under protected/extensions.
Your extension’s folder must look like the following structure:
protected extensions library Zend … a lot of folders … Open up the index.php file and put the following code:
$zend = dirname(__FILE__) . '/protected/extensions/library'; set_include_path(get_include_path() . PATH_SEPARATOR . $zend);
We’re setting include_path dynamically. Some webservers do not allow change the include_path this way, so probably you’ll need to change the php.ini and sets the include_path to a different location. I’m not planning cover this kind of setup here because there isn’t a standard configuration process.
Well, now we have to implement the autoloader feature for ZendAMF. Open the protected/config/main.php file and put the following code snippet at the beginning of the document, right before the configuration array.
require_once 'Zend/Loader/Autoloader.php'; spl_autoload_unregister(array('YiiBase', 'autoload')); spl_autoload_register(array('Zend_Loader_Autoloader', 'autoload')); spl_autoload_register(array('YiiBase', 'autoload'));
At this point we already have the ZendAMF installed and integrated with Yii. This means you can integrate the ZendAMF server at some controller and provide a action that will process the AMF requests coming from Flex applications.
I’ll clarify everything a little more, let’s configure our protected/config/main.php file and add some paths on the import section:
... 'import' => array( // some paths hidden for brevity 'application.services.*', 'application.dtos.*', ), ...
Now le’s take a look at an example of a controller, service and dto code:
// protected/controllers/RemotingController.php class RemotingController extends Controller { public function actionAmf() { $server = new Zend_Amf_Server(); // Services that will be exposed to process AMF requests $server->setClass('HelloService'); $server->setProduction(true); // DTOs mapped between flex and php $server->setClassMap("HelloDTO", "HelloDTO"); echo $server->handle(); } } // protected/services/HelloService.php class HelloService { public function hello($name) { $dto = new HelloDTO(); $dto->message = sprintf('Hello, $s!', $name); return $dto; } } // protected/dtos/HelloDTO.php class HelloDTO { public $message;
} These 3 classes above shows how easy are create an service with an operation, that in our case are HelloService and the hello operation, which receive a name parameter. The hello operation takes the name parameters and creates an HelloDTO class instance.
This HelloDTO class instance will be serialized/deserialized as it should be and if you have a HelloDTO on your Flex application, correctly annotated with the [RemoteClass(alias="HelloDTO")] metadata, everything should works as expected.
Another additional point that you probably will need to do is configure the amf channels on your Flex application. You can see how to do this on the ZendAMF documentation, the only thing you have to remember is that our AMF’s endpoint, with the configuration covered on this post, resides on the http:{your_webserver}/{your_app_name}/index.php?r=remoting/amf If you have any doubts or troubles during the installation and configuration feel free to comment in this post