Lazy loading classes in Zend Amf w/ addDirectory

November 4th, 2008

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.

$server->addDirectory(dirname(__FILE__) .'/../services/');
$server->addDirectory(dirname(__FILE__) .'/../package/');

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

Flash Platform, Zend_Amf

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.

Flash Platform, Zend_Amf

MAX Scheduale set!

November 2nd, 2008

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.

Speaking

Zend Amf for President ’08

October 31st, 2008

Zend Framework is finally running for higher office!

Flash Platform, Zend_Amf

Lee Brimelow created a Zend Amf tutorial

October 30th, 2008

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.

New video tutorial on using ZendAMF

Lee is not using comments.. ;-)
Doc Bloc is your friend! LOL

Flash Platform, Zend_Amf

Missed the Flex and Zend e-Seminar? No Problem.

October 29th, 2008

Did you miss last week’s Flex and Zend e-Seminar? That’s not a problem – we recorded the session using Acrobat Connect and have posted it online.

If you have questions about using Flex and the Zend Framework and want them answered in person, there will be another session about using Flex and the Zend AMF module in the Zend Framework on November 12th at 10 AM PT. Lee Brimelow and Zend AMF contributor Wade Arnold will be delivering the session.

Repost from Flex Team Blog

Flash Platform, Zend_Amf

Type Mapping in AS3 : WRAP IT UP!

October 28th, 2008

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.

or as Ivan Erickson Suggested:

private function onResult(event:ResultEvent):void{
  try {
    var myContact:ContactVo = ContactVO(event.result);
    trace('name' +myContact.firstname);
  }
  catch(error: Error) {
    trace('onResult error: ' + error.message);
  }
}

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));
}

Hope that this helps!

AMFPHP, Flash Platform, Zend_Amf

Bug Fix: Returning nested arrays in Zend_AMF produces Client.Data.UnderFlow errors

October 27th, 2008

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

bug report ZF-4712

Flash Platform, Zend_Amf

Zend Amf Questions, Features, Demos, Bugs?

October 24th, 2008

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:

  • Service Browser
  • Authentication
  • AMFext

Flash Platform, Zend_Amf

iLog component survey

October 22nd, 2008

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.

Flash Platform