In one of my earlier posts i described a method for uploading files using the RemoteObject class found in the Flex SDK. For the “back end” i used the still very popular amfphp implementation of the AMF protocol.
Wade Arnold the maintainer of amfphp is also involved in another implementation of the AMF protocol which is shipped with the Zend Framework. After reading some blog posts about this “new” package called Zend_Amf, i decided to give it a try.
Download ZendAMFUpload.zip for full example code.
I started my quest with downloading of the latest Zend Framework library, which is now at version 1.7.8 and followed the instructions of the Quickstart guide, which can be found here.
I also found a great post from Piotr Walczyszyn about how to merge the Zend_Amf package within the Zend Framework MVC structure and i used this approach as a starting point for my own example, but made the following changes compared to the tutorial of Piotr:
1)
Because i don’t use a database within my example i removed all code related to the config, database and registry from my bootstrap.php file.
2)
Somehow the disableLayout call in the MessageBrokerController init function Piotr provided resulted in an error, so i removed this line.
3)
Instead of putting my data objects into a dto folder i used a folder named vo. It is the same, but i am used to call my remote data objects Value Objects.
4)
During my quest i stumbled to some strange behaviour when sending Typed Objects (FileVO) from my flex client to the service layer. According to the Zend_Amf documentation you could use $_explicitType within a ValueObject for mapping serverside PHP classes with AS3 classes, but this only worked for retrieving typed objects, not for sending typed objects. Finally i found these issues related to class mapping of typed objects: ZF-5755, ZF-5885 and ZF-6130, and decided to use the $server->setClassMap call in my MessagebrokerController.
Here is the code i ended up with:
MessagebrokerController.php
class MessagebrokerController extends Zend_Controller_Action {
// Initialise controller with no view renderer
public function init() {
$this->_helper->viewRenderer->setNoRender(true);
}
// amf action
public function amfAction() {
$server = new Zend_Amf_Server();
$server->addDirectory(APPLICATION_PATH . '/services/');
// Mapping of PHP FileVO class and AS3 FileVO class
$server->setClassMap("FileVO", "FileVO");
echo($server->handle());
}
}
FileVO.php
class FileVO {
public $filename;
public $filedata;
}
RemoteFile.php
class RemoteFile {
/**
* Upload files!
*
* @param $file as a FileVO Object
* @return string
**/
public function upload($file) {
$data = $file->filedata;
file_put_contents( APPLICATION_PATH . '/uploads/' . $file->filename, $data);
return 'File: ' . $file->filename .' Uploaded ';
}
}
I kept the flex code the same as in my earlier post except for the remote service endpoint url, which should be now something like
http://%5Bsome-host%5D/messagebroker/amf
Download ZendAMFUpload.zip for full example code.
Great article!
I suggest you change the title, because you made the integration of Zend MVC and Zend AMF, I looked this example and is not easily found.
was having trouble with this – was not liking the FileVO at all.
changed the remote class to this and all worked fine
public function upload($filename, $filedata) {
file_put_contents( ‘uploads/’ . $filename, $filedata);
return ‘File: ‘ . $filename .’ Uploaded ‘;
}
Thanks so much for sharing this! It is hard to find pure AS3 examples leveraging the Flex framework. Especially ones decoupled as nicely as this, nice work!
Are we able to use the Progress Bar componente when uploading using ZendAMF? I mean, are we able to use a Progress Event or will we have to try a manual approach?
sadly AMF was created as a messaging protocol and only has request and response therefore it doesn’t send progress events to the client. AMF its recomended for small files or if you really want, i have seen people send smaller sections of a larger file and upload by parts then you can emulate a progress event but i wouldn´t recommend it since is much extra work and there are other ways appart from AMF.
hiya, it seems great, but I wonder, any chance
[…] a little on the net, found a solution that was simpler than I thought based on that article with a few […]
[…] um pouco na net, achei a solução que foi mais simples do que eu imaginava baseada nesse artigo com algumas poucas […]
[…] Researching a little on the net, I found a solution that was simpler than I expected. Based on an article, I only had to make a few […]
Sometimes (but I think it is better to do so to avoid problems with VOs) you should add a mapping string in your Flex application like this:
import flash.net.registerClassAlias;
registerClassAlias(‘FileVO’, FileVO);
Good luck !
Hi, very nice post. Very usefull