CodeIgniter Tip: Accessing CodeIgniter Instance Outside

Sarfraz Ahmed    April 26, 2015 02:17 AM

Sometimes you need to access CodeIgniter application instance completely outside of it, may be in Ajax request or an script which is not part of your main CI application. To do so, you can do this:

// file: CI.php
ob_start();
require_once 'index.php'; // adjust path accordingly
ob_get_clean();
return $CI;

Here we are including index.php file of main CI application. We are using buffering functions here so we don't see actual CI application page and finally we we return the $CI instance so that in any page where we need CI instance, we can simply include this file there. So to use it in some other script, you would simply include above file there:

$CI = require_once 'CI.php';
echo $CI->config->item('base_url'); // test CI instance

That should work for the most cases.

I happen to need CI instance in a cli/console application but above method didn't do the trick for me in console app. So here is another very very hacky way of getting instance of CI in a console app that I was able to come up with after analyzing CodeIgniter code:

// Hacky Way of Accessing CI Instance Outside

error_reporting(1);

$environment = 'development';

$system_path = 'system';

$application_folder = 'application';

if (realpath($system_path) !== false) {
    $system_path = realpath($system_path) . '/';
}

$system_path = rtrim($system_path, '/') . '/';

define('BASEPATH', str_replace("\\", "/", $system_path));
define('APPPATH', $application_folder . '/');
define('EXT', '.php');
define('ENVIRONMENT', $environment ? $environment : 'development');

require(BASEPATH .'core/Common.php');

if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php')) {
    require(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
} else {
    require(APPPATH.'config/constants.php');
}

$GLOBALS['CFG'] =& load_class('Config', 'core');
$GLOBALS['UNI'] =& load_class('Utf8', 'core');

if (file_exists($basepath.'core/Security.php')) {
  $GLOBALS['SEC'] =& load_class('Security', 'core');
}

load_class('Loader', 'core');
load_class('Router', 'core');
load_class('Input', 'core');
load_class('Lang', 'core');

require(BASEPATH . 'core/Controller.php');

function &get_instance() {
    return CI_Controller::get_instance();
}

$class = 'CI_Controller';
$instance = new $class();

return $instance;

As said above, this is extremely hacky way and may or may not work in all cases and you would also need to adjust paths in above code and/or load more libraries/helpers/etc you need by modifying it.







Comments powered by Disqus