0

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).

2
  • 1
    a "normal page" means a controller class? Is Xray "Xray_model" ? Your language is a little unclear - can you explain your structure? Commented Feb 7, 2013 at 21:26
  • I clarified my intent a bit more Commented Feb 7, 2013 at 21:31

3 Answers 3

1

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();
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

I can access the worker.php file from a browser and access anything CI related just fine, but when it's executed from CLI I get errors like PHP Fatal error: Using $this when not in object context. So what is different? It's like the worker.php is detached from everything else because of how it's loaded.
0

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

Comments

0

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.