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

Category:Core | Category:Core::Integration

Learn how to use CI code in other PHP applications.

[h3]Warning[/h3] Please note that this mechanism may lead 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]. Take a look at the relative [url=http://www.codeigniter.com/forums/viewthread/49423/]forum thread[/url]. [h3]Purpose[/h3] 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. [h3]Installation[/h3] No file to download here, you'll have to change the files manually. [h4]hacked ci files[/h4] 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]

[h4]ci_dip controller[/h4] Add the CI_dip controller to your controllers directory. It will basically reconstruct $_GET or $_REQUEST arrays. The controller's file name should be "cidip.php" [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] [h3]Usage[/h3] 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. [h3]POTENTIAL ISSUES[/h3] 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.

[h3]POTENTIAL BENEFITS[/h3]

  • CI code available !
  • CI log process receives PHP messages generated by the external app too.

[h3]EXAMPLES[/h3] [h4]FreakAuth integration[/h4] 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.

[h4][u]THE[/u] little trick[/h4] 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]

NOTE: Short tags are not processed by CI here.

Clone this wiki locally