Jared Eitnier
Jared Eitnier

Reputation: 7152

PHP access CodeIgniter class from file executed by CLI

I have a CI_Model with a Class called Xray. I have a controller class called Pages that handles all my pages within the application. One of these pages is called worker.php. I execute worker.php using Supervisord by the CLI.

I want to be able to access Xray's functions from worker.php, but not through the command line (I won't be using the command line after worker.php is executed).

Upvotes: 0

Views: 932

Answers (3)

Granit
Granit

Reputation: 304

The problem is $this is current null.

I just call the parents constructor first:

function __construct()
{
   parent::__construct();
}

If you inspect parent, you will see class CI_Controller and from there the function __contruct() will help you to boot upp correctly.

Now your $this is an object and you can do

$this->config->load() or $this->load->library('Xray');

Upvotes: 0

Jared Eitnier
Jared Eitnier

Reputation: 7152

Here is the code necessary to include CodeIgniter functionality in a script loaded externally:

ob_start();
include('/path/to/your/index.php');
ob_end_clean();

$ci =& get_instance();
$ci->load->model('xray');

So the problem was that there was no CI instance and therefore nothing would load.

Taken from Ellislabs Forums

Upvotes: 0

jmadsen
jmadsen

Reputation: 3675

Load Xray as either a model or library, whichever is more appropriate, and access normally

class Pages extends CI_Controller {

    function worker()
    {
        $this->load->library('Xray');
        echo $this->xray->my_func();
    }

}

Upvotes: 1

Related Questions