user3232286
user3232286

Reputation:

autoload function not working in codeigniter

I am using codeigniter 3

in application/config/config.php file I have added this autoload code for model

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

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

but it is not working like shown in blog. Do I need anything else to update more for this?

Upvotes: 1

Views: 1332

Answers (2)

Tpojka
Tpojka

Reputation: 7111

Use capital letter for your class name. Btw. I agree with first answer.

Upvotes: 1

AdrienXL
AdrienXL

Reputation: 3008

If you need to autoload a model in your CI3 app, just go in application/config/autoload.php and find the line :

$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();

Upvotes: 1

Related Questions