Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 13 revisions

Category:Integration

[b]WARNING[/b] Please note that this mechanism leads to unstable software. If you want a forum or a wiki for a production environment, have a look at [url=http://expressionengine.com/]Expression Engine[/url].

[url=http://www.codeigniter.com/forums/viewthread/49423/]forum thread[/url]

[b]PURPOSE[/b]

CI allows us to create custom libraries easily. This way we can use some smart classes in our CI applications. Now let's use CI in other PHP applications. We will call 'external app' the application in which you want to use some CI code. This is useful if you want to integrate a forum in your website, and use your authentication process... I'm sure you'll find other ideas.

[b]INSTALLATION[/b]

No file to download here, you'll have to change the files manually.

[b]hacked ci files[/b] In your system directory (or whatever, if somebody has a better idea...), create cidip directory.

Copy index.php to cidip/cidip_index.php At the top of the new file, add those lines: [code] // WARNING: This is not an original CI file // those first lines are a hack to Code Igniter

// backuping query string $query_string = urlencode($_SERVER['QUERY_STRING']); // destroying the $_GET array (but reconstructed when CI leaves) $_GET = array(); // let's use CI a bit to recover our $_GET array $_SERVER['PATH_INFO'] = '/cidip/recover_get_array/'.$query_string; [/code]

Change this line [code] $system_folder = ".."; [/code]

One could comment out [code] error_reporting(E_ALL); [/code] if it ever becomes annoying in the external app (DokuWiki).

Change the last line to [code] require_once BASEPATH.'cidip/cidip_CodeIgniter'.EXT; [/code]

Copy codeigniter/CodeIgniter.php to cidip/cidip_CodeIgniter.php In section Send the final rendered output to the browser (around line 238), comment out [code] $OUT->_display(); [/code]

[b]ci_dip controller[/b] Add the CI_dip controller to your controllers directory. It will basically reconstruct $_GET or $_REQUEST arrays. [code] <?php class CIdip extends Controller {

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

function recover_get_array($query_string = '')
{
    $query_string = urldecode($query_string);
    
    $_SERVER['QUERY_STRING'] = $query_string;
    $get_array = array();
    parse_str($query_string,$get_array);
    foreach($get_array as $key => $val) {
        $_GET[$key] = $this->input->xss_clean($val);
        $_REQUEST[$key] = $this->input->xss_clean($val);
    }
}

} ?> [/code]

[b]USE[/b]

Simply 'require' cidip_index.php where you need CI in your external app. You may have to do that with an absolute path, to avoid bad surprises.

You can use a constant that your external app defines to point to the base path of the app, then navigates with ../ to CI's directory.

[b]POTENTIAL ISSUES[/b]

Well I'm not good enough to be sure of what happens here. If you know, please contribute. The main issue is variables or functions conflicts. You'll have to search for conflicting global vars ($OBJ, $CI, $global, $OUT, $BM, $CFG). If error_reporting is set correctly, PHP will complain about conflicting functions, so there is no problem here.

[b]POTENTIAL BENEFITS[/b]

CI code available !

[b]EXAMPLES[/b]

[b]FreakAuth integration[/b]

Some applications that work with a database of members and authentication can now be integrated to your CI installation. It's even more easy if their authentication process is modular, like in Vanilla or DokuWiki (I'll upload the auth classes later).

I personnaly disabled the login process of the external app (in the config file or removing it from the template...) and use only Freakuth light authentication API login.

[b][u]THE[/u] little trick[/b] Most of the external apps you will integrate uses the auth system very early, so you will often have CI functions available everywhere in the external code (useful in templates to integrate our header or footer views with [code] <?php $obj =& get_instance();echo $obj->load->view('footer',null,true); ?>) [/code]

Clone this wiki locally