Apr 24 / Phil

Codeigniter AMFPHP library – updated

Update 11/24/09: In order to load other libraries/models into your models with AMFPHP, change the BASEPATH declaration in index.php to

define('BASEPATH', realpath(dirname(__FILE__)).'/'.$system_folder.'/');

This is more of a repost of my wiki page on Codeigniter’s site, but i needed filler content for my first few postings on my site.

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 Codeigniter controller

What it does

This AMF library will give you full access to your Codeigniter 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 Codeigniter, or use models outside of your app.  We create a Codeigniter controller, and a Codeigniter AMFPHP library that uses AMFPHP as packaged. – Then you create an AMFPHP Services in the services folder that just extends your Codeigniter models, and gives you full access

Full Working Example

We have packaged up the entire working sample that includes a fresh Codeigniter 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

  • Download Codeigniter and AMFPHP (or pull apart the link above)
  • Make sure Codeigniter works as expected before moving forward
  • Put the unzipped amfphp package into your libraries folder
  • Create your amf_gateway.php in ‘controllers’

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();
 }
}
  • Create a standard Codeigniter model or use an existing one
  • Drop in the amfci_db.php file provided into ‘libraries/amfphp/services/amfci_db.php’
  • Create AMFPHP Service in ‘libraries/amfphp/services’ folder using the template provided

This file is a service for AMFPHP – called from flash
This extends your existing model to give flash access to all its methods

//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;
  }
}

Add the amfci.php library to your library folder 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

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();
  }
}

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);
    }
  }
}

12 Comments

leave a comment
  1. handoyo / Sep 18 2009

    Thanks for the codes…It works with Adobe Air too,doesn’t it? ^_^

    • Phil / Sep 18 2009

      no idea actually…. I would assume so if you put in a complete URL to your gateway.

  2. handoyo / Sep 30 2009

    Ok,thanks a lot…Keep up the nice work..

  3. Mikael B. Örtenheim / Jan 28 2010

    Hey, your library implementation is awesome, however UTF formats( i.e åäöåæø) are not transferred correctly when using your library (though only when you get values and send to flex/flash, when sending values into database it is fine..wierd)

    Is it possible to solve this? I feel like i have tried everything..
    Seems the models are changing this format…can you check to see if you get it too work?

    please help if you can i am sorry to have spammed you here and on the codeigniter forum :)

    regards,

    Mikael

    • Phil / Feb 23 2010

      HI Mikael,

      It was my understanding that AMF is UTF compliant… If the data is coming form your db, 1st step is to make sure your db config file is UTF, as well as your db connection/tables etc.. If that still doesnt work, try adding your characters to a text file on the server, and use your model to read/send that back through… Please let me know what you find out — i may also need to upgrade this library, or just give in and start using the Zend one.

      • Mikael Ortenheim / Feb 24 2010

        Hi thx for the comment :) i solved it by uncommenting the setcharsethandler in your amfci library and setting the charset handler in gateway.php to $gateway->setCharsetHandler(“none”, “UTF-8″, “UTF-8″);

  4. Mikael Örtenheim / Feb 25 2010

    Btw, how do i implement a value object? so that both codeigniter and flex can access them?

    i am presuming one puts them in “services/vo”, but how can i access them from a model in codeigniter?

    db->get(‘product’)->result();
    }

  5. drew / May 6 2010

    How would you do this if you were using a .htaccess file?

    • Phil / May 6 2010

      i use this .htaccess

      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php/$1 [L]

      Then i just set the gateway string to “/amf_gateway”

  6. Noel / May 6 2010

    Hi, thanks a million for this library. It’s really useful. However, I’ve been having some trouble with using libraries and some models.

    I tried your advice above, i.e. changing the basepath, but that kills my entire app, like so;

    Warning: require_once(C:\xampp\htdocs\xxx/C:\xampp\htdocs\xxx/system/codeigniter/CodeIgniter.php) [function.require-once]: failed to open stream: Invalid argument in C:\xampp\htdocs\xxx\index.php on line 124

    Fatal error: require_once() [function.require]: Failed opening required ‘C:\xampp\htdocs\xxx/C:\xampp\htdocs\xxx/system/codeigniter/CodeIgniter.php’ (include_path=’.;C:\xampp\php\PEAR’) in C:\xampp\htdocs\xxx\index.php on line 124

    Does this make sense to you?

    • Phil / May 6 2010

      it looks like somewhere you are double concatenating your path.. . “C:\xampp\htdocs\xxx/C:\xampp\htdocs\….”

  7. Noel / May 7 2010

    Thanks for the response. Yes, that seems to be the issue alright.

    OK, I’ve fixed that. But it’s still not behaving. It works on my localhost, but in a live environment, it doesn’t. It seems to break at the point where I try to call a method from a auto-loaded library, from within a method from a different library I called from inside the service. I think I’m going to have to try a different AMF method :(

Leave a Comment

Security Code: