-
Notifications
You must be signed in to change notification settings - Fork 0
Amfphp and CI
Category:Config | Category:Config::Intergration | Category:Config::Community
This is an easy way to integrate Amfphp with CI, without the need to modify core libraries by using hooks. This article is an expansion of my [url=http://codeigniter.com/forums/editreply/385743/]forum post here[/url] .
It seems that CI will output headers even if no views are defined, causing errors. The following prevents output from displaying when using Amfphp, but allows output for standard page display.
-
Enable hooks in config.php
-
In application/config, add this to hooks.php: [code] $hook['display_override'] = array( 'class' => 'Amfphp', 'function' => 'output', 'filename' => 'amfphp.php', 'filepath' => 'hooks' );[/code]
-
Create a new file in application/hooks called amfphp.php and add this text: [code]<?php class Amfphp { var $ci; function output() { if(!defined('AMFPHP')) { $this->ci =& get_instance(); $this->ci->output->_display($this->ci->output->get_output()); } } } ?>[/code]
-
Create an Amfphp gateway service. This code assumes that your Code Igniter index.php file and the Amfphp gateway folder are in the main web root directory. Optional vars are sent to $_POST, thanks frenzal. [code]<?php class action { function execute($path, $vars=false) { define('AMFPHP', 1);
global $value; if($vars AND is_array($vars)){ // Convert vars to POST data $_POST = $vars; } $_SERVER['PATH_INFO'] = '/'.$path; $_SERVER['QUERY_STRING'] = '/'.$path; $_SERVER['REQUEST_URI'] = '/'.$path; require_once('../../../index.php'); return $value;
} } ?>[/code]
-
Create your controllers. It seems that using hooks solved the problem with loading libraries in the controller functions. [code]<?php if (!defined('AMFPHP')) exit('No direct script access allowed');
class Cont extends Controller {
function Cont()
{
parent::Controller();
}
function func()
{
global $value;
$test = 'Some Stuff';
$value = $test; // $value can be any data type. If it's an array, it should be sent back as an array collection.
}
} ?>[/code]
-
Setup a remote event Flex or Flash to call your controllers. The following is Flex 3 Actionscript code. Create a file called RemoteEvent.as in src/events directory. [code]package events { import flash.events.Event;
public class RemoteEvent extends Event { public var data:*;
public function RemoteEvent(type:String, data:*) { super(type); this.data = data; }
} }[/code]
-
Create an event dispatcher to send the request to Amfphp which calls your CI controller and function. There is probably a better way to implement this, but it works. This is also Actionscript code. Create this file as Remote.as in src/events directory in Flex 3. [code]package events { import flash.events.EventDispatcher;
import mx.collections.ArrayCollection; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.remoting.mxml.RemoteObject;
public class Remote extends EventDispatcher { // Debug private var debug:Boolean = false;
// Event names private var successEventName:String; private var faultEventName:String; // Remote Object private var ro:RemoteObject = new RemoteObject("amfphp"); // Path private var pathItems:ArrayCollection; // Constructor public function Remote(success:Function = null, fault:Function = null) { // Create random name for event dispatch var time:Date = new Date(); var eventName:String = time.getTime().toString() + "_" + Math.round(Math.random()*99999) + 1; // Set success remote events var successName:String = "success_" + eventName; this.successEventName = successName; this.addEventListener(successName, success); ro.addEventListener(ResultEvent.RESULT, this.onSuccess); // Set fault remote events var faultName:String = "fault_" + eventName; ro.addEventListener(FaultEvent.FAULT, this.onFault); if(fault != null) { this.faultEventName = faultName; this.addEventListener(faultName, fault); } } // Build path public function addPath(item:String):void { if(this.pathItems == null) { this.pathItems = new ArrayCollection; } this.pathItems.addItem(item); } // Send remote request public function send(path:String = "", obj:* = null, showBusyCursor:Boolean = true):void { ro.source = "ci.action"; ro.showBusyCursor = showBusyCursor; if(this.pathItems != null) { // Use path array var newPath:String = this.pathItems.source.join("/"); this.ro.execute(newPath, obj); } else { this.ro.execute(path, obj); } } // Success handler private function onSuccess(event:ResultEvent):void { var ci:Object = new Object(); ci = event.result; this.dispatchEvent(new RemoteEvent(this.successEventName, ci)); } // Fault handler private function onFault(event:*):void { if(this.debug == true) { trace("AmfPHP Fault"); } trace(event.message); if(this.faultEventName != null) { this.dispatchEvent(new RemoteEvent(this.faultEventName, event.message)); } }
} }[/code]
-
Make sure you have a file called services-config.xml in the src directory in Flex 3. Replace the codeigniger.com address in endpoint with your domain name and path to gateway.php. It should look like this: [code]<?xml version="1.0" encoding="UTF-8"?>