接著是建立 gateway, 請參考 Using Flex 2 RemoteObject and SabreAMF 和 Getting started with SabreAMF 的作法。
我們會將 gateway 放在 Controllers 裡。Server side remoting 程式則是放在 Models 裡。
在 ci_project/system/application/controllers/ 新增檔案 gateway.php
<?php
class Gateway extends Controller {
function Gateway()
{
parent::Controller();
}
function index()
{
require_once('SabreAMF/CallbackServer.php');
try {
$callbackServer = new SabreAMF_CallbackServer();
$callbackServer->onInvokeService = array($this, 'invokeService');
$callbackServer->exec();
}catch(Exception $e){
echo $e->getMessage();
}
}
function invokeService($service, $method, $data) {
$modelPath = str_replace( '.', '/', $service);
$this->load->model($modelPath, 'amf_model');
return call_user_func_array( array( $this->amf_model, $method ), $data );
}
}
?>
測試的 Models 放在 ci_project/system/application/models/test/test_model.php
<?php
class Test_model extends Model
{
function Test_model()
{
parent::Model();
}
function myMethod($d1, $d2)
{
$ret = array(
array( 'ID' => $d1, 'name' => 'okok', 'phone' => $d2 ),
array( 'ID' => '35712', 'name' => 'Esbd V. Mb', 'phone' => '235-4263' ),
array( 'ID' => '46823', 'name' => 'Ftce W. Nc', 'phone' => '346-5374' ),
);
return $ret;
}
}
?>
Flash CS3 AS3 的 client 測試
import fl.data.DataProvider;
var nc:NetConnection = new NetConnection();
//nc.objectEncoding = ObjectEncoding.AMF3;
var onResult:Function = function(result:Object) {
QopDump.echo(result);
dg.dataProvider = new DataProvider(result);
};
var onStatus:Function = function(info:Object) {
QopDump.echo(info);
};
var responder:Responder = new Responder(onResult, onStatus);
nc.connect("http://localhost/ci/?/gateway");
nc.call("test.Test_model.myMethod", responder, ["888", "666"]);
Flex 的 client 測試
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:RemoteObject id="ro"
destination=" "
endpoint="http://localhost/ci/?/gateway"
source="test.Test_model"
result="resultHandler( event )"
fault="faultHandler( event )">
</mx:RemoteObject>
<mx:Script>
<![CDATA[
import mx.rpc.events.*;
import mx.controls.Alert;
import mx.rpc.*;
private function load():void {
ro.myMethod('first','123-456789');
}
private function resultHandler( event:ResultEvent ):void {
Alert.show( QopDump.go(event.result) );
dg.dataProvider = event.result;
}
private function faultHandler( event:FaultEvent ):void {
Alert.show( event.toString() );
}
]]>
</mx:Script>
<mx:Button click="load()" label="Load" />
<mx:DataGrid id="dg" />
</mx:Application>
沒有留言:
張貼留言