lazy class loading or auto loading is a new feature that I added to Zend Amf that allows you to specify any number of paths that you would like your services to be loaded from. There are some advantages and disadvantages to this. First it really now works exactly like the the services directory did in AMFPHP with the addition of specifying as many directories as you want. The disadvantage is that I added setClass() initially so that you can make sure that the service is available on the first request rather than being searched, loaded, and then instantiated in the middle of an amf call. I recommend that you use setClass() for any of the methods that are needed instantly on the first request such as a DAO. The other hundred services can live happily out of memory until they are needed. Go update from the Zend Framework 1.7 trunk or just go get the new server file and place it in your Zend/Amf/ directory and addDirectories() away! Also thanks for all the suggestions!
documentation
The Zend Amf Server also allows services to be dynamically loaded based on a supplied directory path. You may add as many directories as you wish to the server. The order that you add the directories to the server will be the order that the LIFO search will be performed on the directories to match the class. Adding directories is completed with the addDirectory() method in your endpoint.
When calling remote services your source name can have underscore(_) and dot(.) directory delimiters. When an underscore is used PEAR and Zend Framework class naming conventions will be respected. This means that if you call the service com_Foo_Bar the server will look for the file Bar.php in the each of the included paths at com/Foo/Bar.php. If the dot notation is used for your remote service such as com.Foo.Bar each included path will have com/Foo/Bar.php append to the end to auto load Bar.php
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:
I finally purchased my Adobe MAX ticket and filled out my scheduale. Let me know if you are speaking or attending and read this blog. Would love to attend your session or grab a beer/coffee to chat about the type of things that make you come back to this page! I will make sure I blog, twitter, etc while I am there.
Most of my examples have been produced in Flex and assume that you understand that Zend Framework. Lee produced a new tutorial that starts from scratch that and uses a netconnection out of Flash for the rather than RemoteObject from Flex. If your just learning Zend Amf go check out this video.
Type Cast objects coming back in AMF are mx.utils::ObjectProxy and are capable of becoming the object type that you specified. However we don’t want to JUST ASSume that you are getting the correct object type back. It takes a small amount of work. In order to change the ObjectProxy into the proper data type.I recommend that you wrap your result into a try catch before you just assume that you have received the correct data.
private function onResult(event:ResultEvent):void{
try {
var myContact:ContactVO = event.result as ContactVO;
trace('name' +myContact.firstname);
}
catch(error: Error) {
trace('onResult error: ' + error.message);
}
}
Yes you could just Jam the type into a value.
var myContact:ContactVo = ContactVO(event.result);
But if something didn’t work out on the server and you get a successful result your application just crashed because you have a type correlation exception.
if you’re not going to null check the return from var and save the CPU cycle for the as().. thanks Ivan!
Joel Caspers a new T8DESIGN stud also says: Also, you can remove the need for try-catch statement entirely by using the is operator in an if statement – in my experience try catch statements are processor costly and I’ve always been instructed to stay away from them, as well as you could be catching an error that you are not intending to catch (something other than the cast error inside your try statement).
If (event.result is ContactVO) {
//it is what you think it is
Var myContact:ContactVO = ContactVO(event.result);
} else {
//the result is not what you think it is so spit out the type in the console
Trace(getQualifiedClassName(event.result));
}
Zend Amf has been updated to reflect a bug found by Patrick Gutlich. To get this patch, you have to checkout the latest version of Zend_Amf from standard/trunk in SVN. Thanks Patrick for your help!
Zend Amf is about to become finalized for the 1.7 release. This makes for a good time to figure out what people want to see next. Please post your feedback here if you have been using Zend Amf and need an additional feature to have it meet your needs. Some of the things that I wanted added are:
Take this survey about the ilog components and you could win $500 bones. What’s more important is that the 2.0 release looks pretty cool. There is a 3 minute video in the survey that shows all the new stuff.
Recent Comments