i’m autoloading a library named as ‘render’ in my codeigniter app.
the full code of the library is :
class Render extends CI_Controller {
public function template($template, $view, $extra_css, $extra_js) {
$data = array();
if (isset($view)) {
$data['view'] = $view;
}
if (isset($extra_css)) {
$data['extra_css'] = $extra_css;
}
if (isset($extra_js)) {
$data['extra_js'] = $extra_js;
}
$template = $this->load->view("templates/$template", $data, TRUE);
echo $template;
}
}
this library working fine but the problem is that whenever i load this library manually or by editing autoload file, i get an error when i load any model in any of my controller.
A PHP Error was encountered Severity: Notice Message: Undefined property: Home::$my_model_name Filename: controllers/home.php Line Number: 11
here line number 10 and 11 are :
$this->load->model('my_model_name');
$this->my_model_name->my_model_method();
and i also tried to use :
$this->load->model('my_model_name', 'My_model');
$this->My_model->my_model_method();
My controller “home” code is :
class Home extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->model('my_model_name');
$this->my_model_name->index();
}
}
i tried to add a __construct() method to my library but still no luck.
By doing
you’re not creating a library, but a controller! In order to create a library, just create the class and put it into the libraries folder.
Inside your library, if you want to use CI’s loader to load models, for ex., you need to instantiate the main CI class.
Something like (file
application/libraries/render.php) :Then you can
$CI->loadeverything you want inside your library, models, other libraries, whatever.See Utilize CI resource within your library for a thorough explanation of that. The, you call your library the usual way,
$this->load->library('render')and then$this->render->whatever();