Codeigniter get_instance function from external sc

2019-07-30 02:23发布

问题:

I am calling from the shell a php file that is not in the Codeigniter application folder and so I need an instance of the application and I have used

<?php 

const BASEPATH = "/"; 
require './system/core/Controller.php';

$CI =& CI_Controller::get_instance();
var_dump($CI->load->helper('url'));

but I get error:

PHP Notice:  Trying to get property 'load' of non-object

回答1:

the only thing you've to do is to try to create an instance... The best way to do this is to actually include the index.php - but to suppress the output

the following should work

ob_start();
require_once('./index.php');
ob_end_clean();

var_dump($CI->load->helper('url'));