简体   繁体   English

Codeigniter 3.0.2无法从库文件夹加载应用程序库

[英]Codeigniter 3.0.2 not loading application library from libraries folder

I don't know why codeigniter is showing such behavior: 我不知道为什么codeigniter会显示这种行为:

1) I created a class Adminforms.php in application/libraries/form folder. 1)我在application / libraries / form文件夹中创建了一个Adminforms.php类。

class Adminforms {

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

2)When i am loading this library class in a view 2)当我在视图中加载此库类时

<? $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. 3)我进入了codeigniter的Loader类,开始看到它的行为。 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). 而且codeigniter还在_ci_classes中设置了它的名称(Loader.php第1242行)。 But when it assign object to codeigniter instance 但是当它分配对象到codeigniter实例时

$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. 我尝试为我的Adminforms.php库打印此实例对象$ CI-> $ object_name。 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. 您需要首先获取ci对象,然后改用该对象。 The ci object is not available in the view. ci对象在视图中不可用。 Ideally you shouldn't be loading libraries in the view. 理想情况下,您不应在视图中加载库。 Codeigniter follows MVC pattern. Codeigniter遵循MVC模式。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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