PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Zend_Amf_Server

Zend_Amf_Server provides an RPC-style server for handling requests made from the Adobe Flash Player using the AMF protocol. Like all Zend Framework server classes, it follows the SoapServer API, providing an easy to remember interface for creating servers.

Example 29. Basic AMF Server

Let's assume that you have created a class Foo with a variety of public methods. You may create an AMF server using the following code:

<?php
$server 
= new Zend_Amf_Server();
$server->setClass('Foo');
$response $server->handle();
echo 
$response;

Alternately, you may choose to attach a simple function as a callback instead:

<?php
$server 
= new Zend_Amf_Server();
$server->addFunction('myUberCoolFunction');
$response $server->handle();
echo 
$response;

You could also mix and match multiple classes and functions. When doing so, we suggest namespacing each to ensure that no method name collisions occur; this can be done by simply passing a second string argument to either addFunction() or setClass():

<?php
$server 
= new Zend_Amf_Server();
$server->addFunction('myUberCoolFunction''my')
       ->
setClass('Foo''foo')
       ->
setClass('Bar''bar');
$response $server->handle();
echo 
$response;

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.

<?php
$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 autoload Bar.php

All AMF requests sent to the script will then be handled by the server, and an AMF response will be returned.


All Attached Methods and Functions Need Docblocks

Like all other server components in Zend Framework, you must document your class methods using PHP docblocks. At the minimum, you need to provide annotations for each required argument as well as the return value. As examples:

<?php
// Function to attach:

/**
 * @param  string $name
 * @param  string $greeting
 * @return string
 */
function helloWorld($name$greeting 'Hello')
{
    return 
$greeting ', ' $name;
}
<?php
// Attached class

class World
{
    
/**
     * @param  string $name
     * @param  string $greeting
     * @return string
     */
    
public function hello($name$greeting 'Hello')
    {
        return 
$greeting ', ' $name;
    }
}

Other annotations may be used, but will be ignored.

Zend Framework