[FX.php List] FX.php as a Library in CodeIgniter framework

DC dan.cynosure at dbmscan.com
Thu Apr 12 09:50:14 MDT 2007


Hi,

I put together what I think is the cleanest integration possible between 
FX.php and CodeIgniter (CI). It's pretty easy to do.

If you are looking for a good way of expanding your PHP horizons, 
CodeIgniter is a good way to do it. If you haven't seen CodeIgniter, 
head over to http://codeigniter.com to check it out. In short, it is a 
PHP Framework that helps you organize code and offers nice integrated 
libraries. It is also extendable.

I've been testing out FX.php with CI and I like it a lot.

Here's how I got my set up working:

1) Download, install and configure CI per the very good instructions at 
the site. If you have problems, the CI forum is helpful. It should take 
less than an hour for an intermediate PHP person to get to hello world 
(welcome_message.php). Time: 1 hour.



2) Once you've got CI running, put the latest copy of FX.php, 
FX_Error.php, and FX_constants.php in CI's application/libraries/ 
directory. Time: 5 minutes tops.


3) Now, edit FX.php to add a "wrapper" constructor like this:

/*------------------------------------------------------------------*/
// DEC 20070411 wrapper for CI integration
// needs to be able to init with params in an array
// load these using config file FX.php
function FX ($params) {
	$this->FX_init(
	$params['dataServer'],
	$params['dataPort'],
	$params['dataType'],
	$params['dataURLType']
	);
}

// also, change the name of the FX method to FX_init
function FX_init ($dataServer, $dataPort=591, $dataType='', $dataURLType='')
// DEC 20070411 end wrapper function addition for CI integration
/*------------------------------------------------------------------*/

This wrapper function FX() lets CI load FX.php with an array as an 
argument. External libraries like FX.php need slight modifications like 
this to integrate properly.

Please note... this shows the *only code I changed* in FX.php. In 
summary, you need a new function called FX() which will be the new 
constructor. It translates the array passed in by the CI class loader 
into something the normal FX.php constructor can understand.

Also, you need to change the old FX constructor to FX_init - this is 
very important. If you have two constructor functions with the same name 
in a class, strange things arise. Time: Another 5 minutes, tops.




4) Make a file called FX.php (the same name as the class so CI can load 
it automatically) and put it in CI's application/config/ directory. It 
should contain the data that is normally found in server_data.php in the 
FX.php distribution but formatted like a PHP file like this:

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
|--------------------------------------------------------------------------
| FX FMP database access data
|--------------------------------------------------------------------------
*/

$config['dataServer']='127.0.0.1';
$config['dataPort']='591';
$config['dataType']='FMPro5/6'; // or FMPro7
$config['dataURLType']='http';

?>

CI will load this data and pass it to the new FX.php class constructor 
for you. Time: 5 more minutes.




5) Almost there... make a "controller" file called "search.php" and put 
it in the application/controllers/ directory with this PHP code (modify 
the database, layout and field data):

<?php

class Search extends Controller {

function Search()
{
	parent::Controller();	
}

function id($id)
{
	// CI knows where to find the
	// lightly customized FX.php file at
	// CI path: application/libraries/FX.php
	// FMP WPE data (IP, port, server version,
	// and url scheme) is in custom config file at
	// CI path: application/config/FX.php
	$this->load->library('FX');
	// tell FX which DB, layout to use and
	// how many records to return
	$this->fx->SetDBData('database.fp5', 'layout', 1);
	// build a find request, field name, data, comparison
	$this->fx->AddDBParam ('id', $id, 'eq');
	// do find, return dataset, no flattening, meta object
	$res = $this->fx->DoFXAction(FX_ACTION_FIND, TRUE, TRUE);
	echo '<pre>';
	print_r($res);
	echo '</pre>';
}
}
?>

This AddDBParam example assumes you have a database with the fieldname 
'id' - you, of course, can change that to suit. I suggest just pointing 
the FX requests to a database you've already got set up and working. 
Time: If you use a database already set up, 5 minutes to modify this 
code and fire it up.


6) Point your browser to:
http://example.com/path_to_CI/index.php/search/id/1

Note: if you followed the CI install instructions on how to use an 
.htaccess file to use mod_rewrite for nice urls, then you can drop the 
index.php and just use:

http://example.com/path_to_CI/search/id/1

CI takes the URL and turns it into a variable for your controller 
function to use as a search parameter. Nice and sweet.

Let me know if you have problems with this. I'd like to get a little 
feedback from the FX folks first and then post instructions at the CI 
forums.

Did it take you an hour and 20 minutes? Let me know!

dan


More information about the FX.php_List mailing list