Bootstrap file for Zend Amf
November 4th, 2008
There seems to be lots of questions about the best way to setup your bootstrap/endpoint/index.php file in order to work with Zend Amf. The idea of leveraging the Zend MVC framework to route to your server endpoint is a common question but personally seems like a bad idea. Although it may seem convenient it does not provide any MVC integration and second will slow down the response time of your service greatly.
I typically have something like the following structure:
public/
.htaccess
index.php
xmlrpc.php
jsonrpc.php
amf.php
In my .htaccess, I have the following:
php_value date.timezone "UTC"
php_value short_open_tag 1
php_value error_reporting 8191
php_flag display_errors Off
php_flag display_startup_errors Off
# Rewrite Rules
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^/?xmlrpc xmlrpc.php [NC,L]
RewriteRule ^/?jsonrpc jsonrpc.php [NC,L]
RewriteRule ^/?amf amf.php [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Then, in my amf.php, I’d have the following:
setClass(...); // etc...
echo $server->handle();
The rewrite rules allow you to go directly to the given file, and then each file sets up the include_path, autoloading, and the server.


Where the heck was that last weekend at 3 in the morning? Seriously, Thanks for posting this. So this gives us access to the framework via the index and the other server through their respective url right? Would this be the recommended approach for other Zend Servers also?
Yes this was recommended to me by Matthew Weier O’Phinney and I just assume that he speaks the truth.
Hello Wade,
I have a problem with class mapping. I try to use the remoting example (zend-php-samples) with ContactDAO class as following :
bootstrapamf.php :
$server = new Zend_Amf_Server();
$server->setClass( “ContactDAO” );
$server->setClassMap( “ContactDTO”, “Contact” );
Everything works fine, but in ContactDAO, function getContacts() returns an array of contacts but in Flex they are not typed.
ContactDTO.as :
[RemoteClass(alias="Contact")]
[Bindable]
public class ContactDTO
{
public var id : int;
public var firstname : String;
public var lastname : String;
public var email : String;
public var mobile : String;
}
Any help on this please ?
How about using Zend_Db::factory() inside the bootstrap file?
I blogged on utilizing Zend_Db and Zend_Config for Zend_Amf:
http://blog.log2e.com/2008/11/26/whats-in-the-zend-framework-for-the-flashflex-developer/
In case you want to build a hybrid application with both Flex and HTML interfaces, it’s not a good idea to put all Zend AMF-related code inside the bootstrap file. For more flexibility, I suggest to set up the Zend AMF server inside a regular Zend controller that can be used as AMF gateway:
http://blog.log2e.com/2008/12/14/utilizing-the-zend-amf-server-inside-a-zend-controller/
@Stefan Schmalhaus What is the benefit for a hybrid application in using the controller to handle the amf request?
@Stefan Schmalhaus:
i think your solution is quite clever. i’ve tested it and it just works fine. but then i’ve read wades comment, which insecured me a little bit …
@wadearno:
i understand you (rhetorical) question cause it’s obvious, that the indirect call “will slow down the response time of your service”. but for me as a newbee to zend_amf and zend framework the question comes: why zend_amf is a part of zend framework, if i should not use the mvc integration of the zend framework?
please don’t understand me wrong. i realy love zend_amf cause it’s much more flexible than amfphp. but it makes me feel uncomfortable if zend_amf has to be treated different than the rest of the framework …
best regards
jan viehweger
Hi Wade, I am sorry to tell you that I totally agree with Jan Viehweger. I have been trying effortless to integrate the AMF server with the mvc integration, either setting it up in the bootstrap file or a Stefan suggested in a controller. I don’t know how it was done before, but it seemed to me with the newest version I downloaded I had some problems. I couldn’t use echo (server->handle()) before dispatching the controller, because of the HTTP header being different than text/html. And whenever I just put server->handle(), without echo after dispatching the controller I got the page rendered but I didn’t get responses from the amf server. It was a pain hoping to find a complete sample integrating an amf server with the mvc model in the zend framework. (Which is basically the heart of the zend framework). And of course I couldn’t find, because what you expect that would be normal, actually doesn’t work like that. Please warn this in your documentation, and give a technical explanation, so it wouldn’t sound that weir what you are proposing here in this post.
Thanks
@Renato & @jan The good thing about the framework is that there are people with opinions. The bootstrap configuration that I suggested is based on personal preference and feedback from my zend liaison. I don’t believe that this is a right or wrong question but a personal preference. Look at Zend_Amf_Response_Http as that is where the headers are being set. This could be removed but please open a ticket as community feedback so that we can get the community’s opinion on removing the headers from the response.