简体   繁体   English

自动加载功能在Codeigniter中不起作用

[英]autoload function not working in codeigniter

I am using codeigniter 3 我正在使用Codeigniter 3

in application/config/config.php file I have added this autoload code for model 在application / config / config.php文件中,我为模型添加了此自动加载代码

function __autoload($class) {
 if (file_exists(APPPATH."models/".strtolower($class).EXT)) {
    include_once(APPPATH."models/".strtolower($class).EXT);
 }
}

to autoload model 自动加载模型

and I am using model in controller like this 我正在像这样在控制器中使用模型

public function index()
{
    $post = new post();
}

but it is showing error Class 'post' not found 但是显示错误类“ post”未找到

I do have post model in model folder already created 我确实已经在模型文件夹中创建了帖子模型

I am using the autoload code from source http://code.tutsplus.com/tutorials/6-codeigniter-hacks-for-the-masters--net-8308 我正在使用来自源代码http://code.tutsplus.com/tutorials/6-codeigniter-hacks-for-the-masters--net-8308的自动加载代码

but it is not working like shown in blog. 但它的工作方式不像博客中所示。 Do I need anything else to update more for this? 我是否还需要其他任何更新?

If you need to autoload a model in your CI3 app, just go in application/config/autoload.php and find the line : 如果需要在CI3应用程序中自动加载模型,只需进入application / config / autoload.php并找到以下行:

$autoload['model'] = array();

Then, add the model you want to autoload : 然后,添加您要自动加载的模型:

$autoload['model'] = array('my_model', 'my_second_model');

Then in your controller, you don't need to create a new instance of your model class. 然后,在您的控制器中,您无需创建模型类的新实例。 Example : 范例:

$res = $this->my_model->myfunction();

Use capital letter for your class name. 使用大写字母作为您的班级名称。 Btw. 顺便说一句。 I agree with first answer. 我同意第一个答案。

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

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