实现FLEX 通过AMF跟PHP通信

来源:互联网 发布:6x6摇头矩阵灯 编辑:程序博客网 时间:2024/05/01 12:50

AMF  flex一种远调用协议

目标:目前基本使用AMF3 初步会使用flex编写基本程序 熟悉RPC调用

queryphp框架 目前目前是国内最强大ORM类之一,还附带国内最强的通用权限系统

通用权限系统看这里
http://bbs.chinaunix.net/thread-1691979-1-1.html

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

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

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

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

crossdomain.xml内容如下
我在本地虚拟了一个域名guofang.com

  1. <?xml version="1.0"?>
  2. <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
  3. <cross-domain-policy>
  4.    <allow-access-from domain="*.guofang.com" />
  5. </cross-domain-policy>
复制代码

我们在framework/lib/services 建一个HelloWorldService.class.php文件
没有services目录自己建一个就可以了插件会搜索这里的

HelloWorldService.class.php内容如下

  1. class HelloWorldService extends sfAmfService {
  2.      public function sayHello($who) {
  3.       return "Hello ".$who; //直接返回名字
  4.     }
  5. }
复制代码

我们只要几行代码就可以调用服务文件了。我们在某个项目中调用比如project/router目录下面

路由文件调用内容如下amfRouter.class.php

  1. <?php
  2. class amfRouter extends controller{
  3.   public function index()
  4.   {
  5.      //加载amf插件
  6.      import('@plugin.amf.sfAmfGateway');
  7.      //调用amf插件
  8.      $gateway = new sfAmfGateway();
  9.      //输出内容 $gateway->service();为返回内容
  10.      //handleRequest 中自动调用 header(SabreAMF_Const::MIMETYPE);
  11.      //因为我没有别的内容输出了所以直接输出内容
  12.      $gateway->handleRequest();
  13.      Return 'ajax';
  14.   }
  15. }
  16. ?>
复制代码

访问地址记下来,我的是 http://www.guofang.com/project/amf/index


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

flex 前端文件程序

目标:学会编写flex 程序



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

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

我们创建一个amftest项目

amf1.gif



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

amftest.mxml内容如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  3.                            xmlns:s="library://ns.adobe.com/flex/spark"
  4.                            xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
  5.                 <fx:Script>
  6.                         <![CDATA[
  7.                         import mx.events.CloseEvent;
  8.                         import mx.controls.Alert;
  9.                         import mx.rpc.remoting.mxml.RemoteObject;
  10.                         import mx.managers.CursorManager;
  11.                         import mx.rpc.events.ResultEvent;
  12.                         import mx.rpc.events.FaultEvent;
  13.                         private function sayHello():void {
  14.                         var remote:RemoteObject = new RemoteObject("helloworld");
  15.                         remote.source = "HelloWorldService";
  16.                                 remote.addEventListener("result", function (event:ResultEvent):void {
  17.                                 result.text = event.result.toString();
  18.                                 });
  19.                                 remote.addEventListener("fault", function(event:FaultEvent):void {
  20.                                 Alert.show(event.fault.toString(), "Error");
  21.                                 });
  22.                         remote.sayHello(username.text);
  23.                         }
  24.                         ]]>
  25.                 </fx:Script>
  26.         <fx:Declarations>
  27.         </fx:Declarations>
  28.         <s:TextInput x="246" y="88" id="username"/>
  29.         <s:TextInput x="245" y="140" id="result"/>
  30.         <s:TextInput x="246" y="191" id="fault"/>
  31.         <s:Button x="423" y="85" click="sayHello();" label="按钮"/>
  32.         <s:Label x="171" y="88" text="输入名字" width="63" height="24"/>
  33.         <s:Label x="179" y="140" text="结果" width="58" height="22"/>
  34.         <s:Label x="182" y="195" text="失败" width="58" height="22"/>
  35. </s:Application>
复制代码

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


amf2.gif




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


services-config.xml内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <services-config>
  3.   <services>
  4.     <service id="helloworld-service"
  5.           class="flex.messaging.services.RemotingService"
  6.           messageTypes="flex.messaging.messages.RemotingMessage">
  7.         <destination id="helloworld">
  8.           <channels>
  9.             <channel ref="helloworld-channel"/>
  10.           </channels>
  11.           <properties>
  12.             <source>*</source>
  13.           </properties>
  14.         </destination>
  15.     </service>
  16.    <service id="flextest-service"
  17.           class="flex.messaging.services.RemotingService"
  18.           messageTypes="flex.messaging.messages.RemotingMessage">
  19.         <destination id="flextest">
  20.           <channels>
  21.             <channel ref="flextest-channel"/>
  22.           </channels>
  23.           <properties>
  24.             <source>*</source>
  25.           </properties>
  26.         </destination>
  27.     </service>
  28.   </services>
  29.     <channels>
  30.           <channel-definition id="flextest-channel"
  31.             class="mx.messaging.channels.AMFChannel">
  32.         <endpoint uri="http://www.guofang.com/project/amf/index"  class="flex.messaging.endpoints.AMFEndpoint"/>
  33.       </channel-definition>
  34.       <channel-definition id="helloworld-channel"
  35.             class="mx.messaging.channels.AMFChannel">
  36.         <endpoint uri="http://www.guofang.com/project/amf/index"  class="flex.messaging.endpoints.AMFEndpoint"/>
  37.       </channel-definition>
  38.     </channels>
  39. </services-config>
复制代码

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

amf3.gif



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

amf4.gif




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

附flex程序
amftest.zip (1.39 MB)