Zend_Amf_Server
2009年03月18日 07:43
Zend Framework + AS3/Flex ユーザーの自分にとっては Zend と Adobe が提携して Zend_Amf_Server がリリースされたので、おっ!って感じで早速使ってみた。
これまでは AMF 通信の際は AMFPHP 1.9 を使っていて特にこれで問題は無いけれど、Zend Framework がインクルードされている環境では Zend_Amf_Server はすぐに使えるし、個別にパッケージをダウンロード出来るのでそれだけアップロードしても使用可能。Zend Framework ベースでデータベース連携とかするなら PHP 5 / MySQL 5 とか推奨なので、環境が無い場合はこれまで通り AMFPHP + PEAR 辺りで Flash と連携して特に問題ないと思う。一応、Cairngorm Framework とともに使ってみたけど使い勝手に特に問題ないし、外国人の開発陣も結構興奮している模様なんで、次のアプリケーション開発で環境が整う場合には、設定とか簡単だし Zend_Amf_Server を使ってみよう。
使い方。
(1)任意のサーバディレクトリに server.php(名前は何でも OK。)を作成し、下記の様に AMFServer を作成する。ブラウザ経由でこのページに直接アクセスしダウンロードが開始されるか、システムコマンドでアクセスして <p>Zend Amf Endpoint</p> と表示されたら設定完了。ただしこのままだと AMFServer が設定されただけなので、実際に Flash/Flex から呼び出す関数を持った PHP class(ここでは、Test.class.php)を setClass 関数で AMFServer インスタンスにアタッチする。また、名前空間を使用し複数のクラスをセットしたり、特定のディレクトリごとすべて含める事が出来る。これを設定する事で、Flex 側から RemoteObject の source プロパティで PHP 側のクラスファイルを記述する必要が無くなる。また、setClassMap 関数で明示的に DTO にも対応させることができるのは AMFPHP とは設定は多少違う。
AMFServer 用の php ファイル設定(http://あなたの URL/server.php)
<?php
require_once('Zend/Amf/Server.php');
require_once('class/Test.class.php');
// Zend_Amf_Server Object
$server =& new Zend_Amf_Server();
$server->setClass('Test');
//$server->setClassMap('TestVO', 'TestVO');
$response =& $server->handle();
echo $response;
?>
Test class(AMFPHP 用クラスファイル)
*今回は簡単な helloWorld 関数を持った Test クラスを作成する。
<?php
class Test {
/**
* Construct Function
*/
public function __construct() {
}
/**
* Function to helloWorld
* @param String name
* @return String
*/
public function helloWorld($name') {
return 'Hello, ' . $name;
}
}
?>(2)Flex からの呼び出し
Flex の場合
*services-config.xml の設定方法はここをどうぞ。
<?xml version="1.0" encoding="UTF-8"?>
<services-config>
<services>
<service id="zend-service" class="flex.messaging.services.RemotingService" messageTypes="flex.messaging.messages.RemotingMessage">
<destination id="zend">
<channels>
<channel ref="zend-endpoint"/>
</channels>
<properties>
<source>*</source>
</properties>
</destination>
</service>
</services>
<channels>
<channel-definition id="zend-endpoint" class="mx.messaging.channels.AMFChannel">
<endpoint uri="http://あなたの URL/server.php" class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
</channels>
</services-config>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:RemoteObject
id="testService"
destination="zend"
showBusyCursor="true"
result="resultHandler(event)"
fault="faultHandler(event)" />
<mx:Button id="testButton" label="test" click="onTest(event)" />
<mx:Script>
<![CDATA[
private function resultHandler(evt:ResultEvent):void {
// Hello, convexstyle と trace されると成功
trace(evt.result);
}
private function faultHandler(evt:FaultEvent):void {
trace("faultCode: " + evt.fault.faultCode);
trace("faultDetail: " + evt.fault.faultDetail);
trace("fault: " + evt.fault.faultString);
}
private function onTest(evt:MouseEvent):void {
this.testService.helloWorld("convexstyle");
}
]]>
</mx:Script>
</mx:Application>
(3)Flash からの呼び出し
package {
import flash.display.MovieClip;
import flash.events.*;
import flash.net.NetConnection;
import flash.net.Responder;
public class Main extends MovieClip {
private static var _gateway:String = "http://あなたの URL/server.php";
private var _connection:NetConnection;
private var _responder:Responder;
public function Main() {
this._responder = new Responder(onResultHandler, onStatusHandler);
this._connection = new NetConnection();
this._connection.connect(Main._gateway);
this.init();
}
private function init():void {
this._connection.call("helloWorld", this._responder, "convexstyle");
}
private function onResultHandler(evt:Object):void {
// Hello, convexstyle と trace されると成功
trace(String(evt));
}
private function onStatusHandler(fault:Object):void {
trace(String(fault.description));
}
}
}
なんつーか、シンプルだなあ。Zend ってのがいい!
