2009年10月8日 星期四

[Codeigniter] Codeigniter 整合 AmfPHP

Codeigniter  搭配開發 FLEX 專案時,要如何透過 Amfphp 傳送資料至前端 FLEX 處理呢?
整合步驟如下:

所需套件:
1. Codeigniter 1.7.1 版本以上
2. AMFPHP 1.9 版本以上

整合流程:
1. 先將解壓縮後的 amfphp 資料夾移至 Codeigniter 開發目錄 (預設路徑為 system\application\) 下的 libraries 目錄裡
2. 建立 amfphp gateway controller 檔案  ( amf_gateway.php )

//This file replaces the need for gateway.php
//setup netConnection to ‘/index.php/amf_gateway’
class Amf_gateway extends Controller
{
  public function __construct()
  {
    parent::Controller();
    //Start our library (keep reading the tutorial)
    $this->load->library('amfci');
    //Set new include path for services
    ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . AMFSERVICES);
  }

//startup the amf gateway service
public function index()
 {
  $this->amfci->service();
 }
} 

3. 建立 amfci_db 檔案至 libraries/amfphp/services/ 目錄下

require_once(BASEPATH.'/../system/libraries/Model.php');


4. 建立 amfci class 檔案至 libraries 目錄下


//This file is the new amfphp library for CI
//It it loaded from the amf_gateway controller, and binds all of it together
class Amfci
{
  public $gateway;
  public $CI;
  public function __construct()
  {
    $this->CI = get_instance();
    require realpath(dirname(__FILE__))."/amfphp/globals.php";
    require realpath(dirname(__FILE__))."/amfphp/core/amf/app/Gateway.php";
    define('AMFSERVICES', realpath(dirname(__FILE__))."/amfphp/services");

    $this->gateway = new Gateway();
    $this->gateway->setCharsetHandler("utf8_decode", "ISO-8859-1", "ISO-8859-1");
    $this->gateway->setLooseMode();
    $this->gateway->setErrorHandling(E_ALL ^ E_NOTICE);
    $this->gateway->setClassMappingsPath(AMFSERVICES.'/vo');
    $this->gateway->setClassPath(AMFSERVICES);

    if(PRODUCTION_SERVER)
    {
      //Disable profiling, remote tracing, and service browser
      $this->gateway->disableDebug();
    }
  }

  public function service()
  {
    $this->gateway->service();
  }
}
 
5.完成以上四個步驟後,基本的 amfphp 整合已大致完成,接下來就是對應前端的 Service 開發過程
 1.首先在 libraries\amfphp\services 下建立 amfphp Service 
2.載入所需的 model class
3.透過 service 繼承 model 取用存取後端資料方法

require_once('amfci_db.php');

// Use this path if you are using a module (ie, MatchBox)
// require_once(AMFSERVICES.'/../../../modules/test_shop/models/product_model.php');
require_once(AMFSERVICES.'/../../../models/product_model.php');
class product_service extends Product_model
{

}
 
6.最後建立 Flex 專案利用 RemoteObject 建立 amfphp gateway 取用 Service 

沒有留言: