实现FLEX 通过AMF跟PHP通信

uuq 2010-10-03 12:37:44
AMF flex一种远调用协议

目标:目前基本使用AMF3

queryphp amf插件从symfony 的sfAmfPlugin移植过来

amf调用跟 远程过程调用(RPC)差不多。

服务文件放在框架项目 lib/services
或框架目录lib framework/lib/services 没有建一个

如果flex生成的swf和amf调用地址不在同域名,请放一个
crossdomain.xml 文件在URL根目录

crossdomain.xml内容如下
我在本地虚拟了一个域名guofang.com
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*.guofang.com" />
</cross-domain-policy>
我们在framework/lib/services 建一个HelloWorldService.class.php文件
没有services目录自己建一个就可以了插件会搜索这里的

HelloWorldService.class.php内容如下
class HelloWorldService extends sfAmfService { 
public function sayHello($who) {
return "Hello ".$who; //直接返回名字
}
}
我们只要几行代码就可以调用服务文件了。我们在某个项目中调用比如project/router目录下面

路由文件调用内容如下amfRouter.class.php
<?php 
class amfRouter extends controller{
public function index()
{
//加载amf插件
import('@plugin.amf.sfAmfGateway');
//调用amf插件
$gateway = new sfAmfGateway();
//输出内容 $gateway->service();为返回内容
//handleRequest 中自动调用 header(SabreAMF_Const::MIMETYPE);
//因为我没有别的内容输出了所以直接输出内容
$gateway->handleRequest();
Return 'ajax';
}
}
?>
访问地址记下来,我的是 http://www.guofang.com/project/amf/index


现在我们做前端部分flex 程序

flex 前端文件程序

目标:学会编写flex 程序



其实感觉flex比js好多了,不会有浏览器兼容问题
而且还自带虚拟机,UI很漂亮
缺点是不能到处画UI,不像js那么方便,特别是jQuery很方便做动画效果

我们可以在网上下载flex builder4 安装好,和输入注册码

我们创建一个amftest项目



创建好mxml文件注意命名input的ID名字,如下面样子

amftest.mxml内容如下

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

<fx:Script>
<![CDATA[
import mx.events.CloseEvent;
import mx.controls.Alert;
import mx.rpc.remoting.mxml.RemoteObject;
import mx.managers.CursorManager;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
private function sayHello():void {
var remote:RemoteObject = new RemoteObject("helloworld");
remote.source = "HelloWorldService";
remote.addEventListener("result", function (event:ResultEvent):void {
result.text = event.result.toString();
});
remote.addEventListener("fault", function(event:FaultEvent):void {
Alert.show(event.fault.toString(), "Error");
});
remote.sayHello(username.text);
}
]]>
</fx:Script>
<fx:Declarations>
</fx:Declarations>
<s:TextInput x="246" y="88" id="username"/>
<s:TextInput x="245" y="140" id="result"/>
<s:TextInput x="246" y="191" id="fault"/>
<s:Button x="423" y="85" click="sayHello();" label="按钮"/>
<s:Label x="171" y="88" text="输入名字" width="63" height="24"/>
<s:Label x="179" y="140" text="结果" width="58" height="22"/>
<s:Label x="182" y="195" text="失败" width="58" height="22"/>

</s:Application>
services-config.xml是Remoting设置文件,本测试我们只操作这两个文件就可以了,注意services-config.xml可以导进来
在编译时候我们要把services-config.xml编译上去 加上:-services services-config.xml参数




//多个service 服务文件URL可以如下这样添加
//红色表示amf文件地址,注意换成你自己的域名
//上面var remote:RemoteObject = new RemoteObject("helloworld"); 中的helloworld就是下面id="helloworld"名


services-config.xml内容如下
<?xml version="1.0" encoding="UTF-8"?>
<services-config>
<services>
<service id="helloworld-service"
class="flex.messaging.services.RemotingService"
messageTypes="flex.messaging.messages.RemotingMessage">
<destination id="helloworld">
<channels>
<channel ref="helloworld-channel"/>
</channels>
<properties>
<source>*</source>
</properties>
</destination>
</service>
<service id="flextest-service"
class="flex.messaging.services.RemotingService"
messageTypes="flex.messaging.messages.RemotingMessage">
<destination id="flextest">
<channels>
<channel ref="flextest-channel"/>
</channels>
<properties>
<source>*</source>
</properties>
</destination>
</service>
</services>
<channels>
<channel-definition id="flextest-channel"
class="mx.messaging.channels.AMFChannel">
<endpoint uri="http://www.guofang.com/project/amf/index" class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
<channel-definition id="helloworld-channel"
class="mx.messaging.channels.AMFChannel">
<endpoint uri="http://www.guofang.com/project/amf/index" class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
</channels>
</services-config>

我们编译调试之前还要设置下服务器,当然可以编译出来再拷过去就可以的



我们开始编译了。如果修改了最好是清除项目已生成的文件
项目->clean选择你的项目就可以了
将会生成如下图样子。浏览器访问
http://www.guofang.com/flex/amftest.html
就可以看到生成好的flex了,可以测试有没有问题



可以到http://code.google.com/p/queryphp/downloads/list
下载queryphp1.0 beta2

...全文
459 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
船员 2010-12-22
  • 打赏
  • 举报
回复
需求中。

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧