Skip to content

AMF Codeigniter Library No Zend No Hooks Etc.

Derek Jones edited this page Jul 5, 2012 · 11 revisions

This is a clean Codeigniter AMFPHP Library, we do not use Zend, Hooks, bootstraps or hacks - really, we promise.

At first glance there is a lot to digest, but it is actually a pretty simple installation and is basically just replacing the gateway file with a CI controller

What it does this AMF library will give you full access to your CI Models, as well as any libraries. In other words, it was written to work with Codeigniter, as Codeigniter was intended to be used - and you can drop this into your existing setup.

How?? Instead of trying to bootstrap CI, or use models outside of your app. We create a CI controller, and a CI AMF library that uses AMF as packaged. - Then you create an AMF Services in the services folder that just extends your CI models, and gives you full access

Full Working Example We have packaged up the entire working sample that includes a fresh CI 1.7.1, AMF 1.9 and 2 flash samples modified from the Open Source Flash Development book chapter by ( Wade Arnold) and a sample product mysql table.

All of the sample code assumes you ARE NOT using a .htaccess file, and also assumes you are using the default everything. If you have moved your app and system path outside your webroot (as you should) it should still work fine too.

Dowload the files

And away we go: Instructions / Explanation of whats going on

Step 1: Download CI and AMFPHP (or pull apart the link above) Step 2: Make sure CI works as expected before moving forward Step 3: Put the unzipped amfphp package into your libraries folder Step 4: Create your amf_gateway.php in 'controllers'

<?php
/*
* This file replaces the need for gateway.php
* setup netConnection to '/index.php/amf_gateway'
* @Author Phil Palmieri ppalmieri [at] page12.com
*/
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();
  }
  
}

Step 5: Create a standard CI model or use an existing one Step 6: Drop in the amfci_db.php file provided into 'libraries/amfphp/services/amfci_db.php' Step 7: Create AMFPHP Service in 'libraries/amfphp/services' folder using the template provided

<?php
/*
* This file is a service for AMFPHP - called from flash
* This extends your existing model to give flash access to all its methods
* @Author Phil Palmieri ppalmieri [at] page12.com
*/
//Load CI DB Instance since we are not coming through index.php
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');

// Use this path if you are using standard install
require_once(AMFSERVICES.'/../../../models/your_model.php');
class your_model_service extends your_model
{
  //Since extending your_model, all existing methods are available
  
  //New Methods
  public function yourNewMethod()
  {
    $something = array(); //Create array to send to flash
    return $something;
  }
}

Step 8: Add the amfci.php library to your library folder [quote]The amfci library is used by amf_gateway controller that replaces the default gateway.php file, by creating an instance of the gateway as $this->gateway, and settign the needed options[/quote]

<?php
/*
* This file is the new amfphp library for CI
* It it loaded from the amf_gateway controller, and binds all of it together
* @Author Phil Palmieri ppalmieri [at] page12.com
*/
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();
  } 
}

Step 9: Connect and call from flash

package com.page12.hello_world {
    import flash.display.MovieClip;
    import fl.events.*;
    import flash.events.*;
    import flash.net.NetConnection;
    import flash.net.Responder;
    import flash.net.ObjectEncoding;
    
    public class Main extends MovieClip {
        private var gateway:String = "/index.php/amf_gateway";
        private var connection:NetConnection;
        private var responder:Responder;
        
        public function Main() {
            send_btn.addEventListener(MouseEvent.CLICK, sendData);
            responder = new Responder(onResult, onFault);
            connection = new NetConnection;
            //connection.objectEncoding = ObjectEncoding.AMF3;
            connection.connect(gateway);
        }
        
        public function sendData(e:MouseEvent):void {
            var params = server_txt.text;
            response_txt.text = 'Send '+server_txt.text+' to server';
            connection.call("hello_world_service.say", responder, params);
        }
        
        private function onResult(result:Object):void {
            response_txt.text = String(result);
        }
        
        private function onFault(fault:Object):void {
            response_txt.text = String(fault.description);
        }
    }
}

Step 10: Send us beer money

We use Charles Proxy debugging tool and highly recommedn it when developing with flash.

If you get any weird errors, add soem comments so others can benefit.

Category:Libraries::AMF Category:Contributions::Libraries::AMF

Clone this wiki locally