Accessing CodeIgniter Super Object from External PHP Script (Outside CodeIgniter)

Last updated 2 weeks, 5 days ago | 29 views 75     5

Tags:- PHP CodeIgniter

Accessing CodeIgniter Super Object from External PHP Script (Outside CodeIgniter)

Sometimes, you may need to access CodeIgniter's core features — such as models, libraries, or configuration — from a standalone PHP script that’s not within the standard CodeIgniter controller or application structure.

Examples include:

  • A cron job file in /scripts/cron.php

  • An API entry point separate from normal routing

  • CLI utility scripts or migration tools


What is the CodeIgniter Super Object?

The Super Object in CodeIgniter (usually accessed via $this in controllers and models) is the central point of the framework that gives access to:

  • Loaded models

  • Helpers

  • Libraries (like email, session, form_validation)

  • Configuration

  • Database

In external scripts, this Super Object isn’t available unless manually initialized.


✅ Step-by-Step: Bootstrapping CodeIgniter in an External Script

Let’s say your external PHP file is located at:

/external/run_task.php

Your CodeIgniter app is installed in:

/ci_app/

✅ Step 1: Define a Path to CodeIgniter’s index.php and BASEPATH

// File: /external/run_task.php

define('CI_ENV', 'development'); // optional
$system_path = '../ci_app/system';
$application_folder = '../ci_app/application';

// Define constants that CodeIgniter expects
define('BASEPATH', realpath($system_path) . '/');
define('APPPATH', realpath($application_folder) . '/');
define('VIEWPATH', APPPATH . 'views/');
define('FCPATH', __DIR__ . '/');

// Set $_SERVER and $_ENV if necessary
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['SCRIPT_FILENAME'] = __FILE__;

✅ Step 2: Load the CodeIgniter Core and Bootstrap

// Load the base CodeIgniter core
require_once BASEPATH . 'core/Common.php';
require_once BASEPATH . 'core/CodeIgniter.php';

Note: This is simplified. A more stable approach is to load index.php indirectly by replicating its boot process, including loading bootstrap.php.


✅ Step 3: Manually Load Required Classes

Once the CodeIgniter base is loaded, you can instantiate the main $CI object:

$CI =& get_instance();

// Load necessary libraries/models
$CI->load->database();
$CI->load->model('My_model');

// Call model method
$data = $CI->My_model->get_data();

print_r($data);

Full Working Example

File: /external/run_task.php

<?php

define('CI_ENV', 'development');

// Paths to your CodeIgniter installation
$system_path = '../ci_app/system';
$application_folder = '../ci_app/application';

// Required base definitions
define('BASEPATH', realpath($system_path) . '/');
define('APPPATH', realpath($application_folder) . '/');
define('VIEWPATH', APPPATH . 'views/');
define('FCPATH', __DIR__ . '/');

// Set up server variables
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['SCRIPT_FILENAME'] = __FILE__;

// Load CI base
require_once BASEPATH . 'core/Common.php';
require_once BASEPATH . 'core/CodeIgniter.php';

// Access the CI Super Object
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->database();
$CI->load->model('My_model');

// Use the model
$data = $CI->My_model->get_data();
print_r($data);

Example Model: application/models/My_model.php

<?php
class My_model extends CI_Model {
    public function get_data() {
        return $this->db->get('my_table')->result();
    }
}

Tips

  1. Set error reporting at the top of your script:

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
  2. Use relative paths carefully, or better yet, use realpath() to resolve absolute paths to avoid file inclusion issues.

  3. Use $CI =& get_instance(); instead of creating new instances to access libraries/models loaded via the Super Object.

  4. Keep the external script light — offload logic to models or libraries so you follow MVC.


⚠️ Common Pitfalls

  • Incorrect base paths: Relative paths may fail. Use realpath() or test with __DIR__.

  • Missing get_instance() call: Without this, you can’t access $CI and will get errors like "Call to a member function on null".

  • Directly instantiating models: Avoid doing new My_model(); — instead, let CodeIgniter handle it via $CI->load->model().

  • Trying to load controllers: You typically shouldn't instantiate controllers in external scripts. Use models or libraries instead.

  • Wrong PHP version or file permissions: Always ensure external scripts match the PHP version of your CodeIgniter app.


Use Cases

  • Cron jobs fetching/syncing data

  • Background workers processing queues

  • Data import/export scripts

  • Setup scripts or one-time migrations


Conclusion

Accessing CodeIgniter’s Super Object from an external PHP script allows you to leverage your existing models, configurations, and database layers without duplicating code or logic. It’s a powerful technique, especially for cron jobs or CLI tools — just be cautious with path setup and Super Object access.