简体   繁体   中英

Codeigniter 3.0.2 not loading application library from libraries folder

I don't know why codeigniter is showing such behavior:

1) I created a class Adminforms.php in application/libraries/form folder.

class Adminforms {

public function displayLoginForm() {
$formOptions = array();
return $formOptions;
}
}

2)When i am loading this library class in a view

<? $this->load->library("form/adminforms"); ?>
<? var_dump($this->adminforms); ?>
<?= $this->adminforms->displayLoginForm(); ?>

It displays error:

A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$adminforms

3) I moved into Loader class of codeigniter and started seeing its behaviour. I seen that file path, class name are perfectly fine to load this library. And codeigniter also sets its name in _ci_classes (Loader.php line 1242). But when it assign object to codeigniter instance

$CI->$object_name = isset($config)
? new $class_name($config)
: new $class_name();

I tried to print this instance object $CI->$object_name for my Adminforms.php library. And I get following:

object(Adminforms)#20 (0) { }

Do anyone have idea why it is not loading class object properly?

You need to first get the ci object then use that object instead. The ci object is not available in the view. Ideally you shouldn't be loading libraries in the view. Codeigniter follows MVC pattern. Business logic should be in Model, control should be in the controller and view should be, well view

$this->ci =& get_instance (  );
$this->ci->load->library("Test/Adminforms");
$this->ci->adminforms->displayLoginForm();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM