Creating Remoting methods and classes

AMFPHP uses a simple scheme to load the appropriate service when requested. Dots in the service name are mapped to slashes (folder structure), and then it appends a .php extension and tries to find the file in the services folder. For example:

this.service = new Service(this.gatewayUrl, null, "com.example.MyService", null, null);

When the gateway is called, it will try to find the MyService.php file in the {servicefolder}/com/example folder. The gateway expects a class defined in that file with the same name as the service. That is, in this example, the php file must contain a class called MyService. While Windows is not case-sensitive, your deployment server may very well be. So make sure the cases match both ways.

The methodTable

In your service class constructor, you need to tell amfphp which methods to export to Flash by defining a member variable, methodTable. Here is a typical methodTable:

<?php
class MyClass{
	function MyClass()
	{
		$this->methodTable = array(
 
			"doStuff" => array(
				"description" => "Puts the value into a session variable with name key",
				"access" => "remote
			),
			"doAnotherThing" => array(
				"description" => "Retrieves the value of the session variable",
				"access" => "remote"
			)
		);
	}
 [methods doStuff and doAnotherThing below]
}
?>

The only required thing is the access key which is set to remote. That means to make this class available to Remoting. The methodTable may also have much more meta information embedded, including arguments, return types, pagesize for pageable recordsets, class mappings, etc. The meta-information can then be used by AMFPHP's code generator to create stub actionscript, while other tags can trigger special functionality like authentication.

The MethodTable class

If you have more than a few methods in your class, the methodTable may quickly become bulky. You will probably want to put it in a separate file and use include it from the constructor. The methodTable may also be generated by the MethodTable class, which uses your JavaDoc comments to generate the methodTable. To use the MethodTable class, first include it at the top of your class:

include(AMFPHP_BASE . "util/MethodTable.php");

Then write this in your constructor:

$this->methodTable = MethodTable::create(__FILE__);

Then all you need to do is write javadoc comments on top of your methods. The following tags are supported:

  • @desc
  • @access (set to remote to export a class)
  • @roles (comma-separated)
  • @instance
  • @returns
  • @pagesize

Using the MethodTable class during development is recommended as having to modify the methodTable all the time is a major pain and a waste of time. You should hardcode the methodTable before deployement for performance. You can do this by browsing to your method using the HTML service browser and clicking on the 'MethodTable' link next to the class name.


© amfphp.org | Disclaimer | Conditions of use