[php] CodeIgniter PHP Model Access "Unable to locate the model you have specified"

I have been trying to load some models for this website I am building. However, for an unknown reason, it will bring the following error :

An Error Was Encountered

Unable to locate the model you have specified: logon_model

Now , I have done my research. The problem would be that IC processes file names in lowercase. However, both my file and the file calling is in lower case, as shown here :

echo "VALIDATING";
            // Validation passed. Off we go to account info verification from AA's database. God help us all.
            $this->load->model('logon_model');
            echo "FOUND MODEL";
            $res = $this->logon_model->verify_user($this->input->post('username'),$this->input->post('password'));
            echo $this->input->post('username');
            echo $this->input->post('password');

The execution does not reach "FOUND MODEL", thus stops on the model loading. I have tried to use:

 $this->load->model(site_url('logon_model'));

With no results. Need to mention the model file is correctly placed in the right model folder ?

How can I fix this ?

EDIT : Header for the model file :

class Logon_model extends CI_Model {

....

This question is related to php model codeigniter-2

The answer is


You can give whatever name you want.

Styles guides are recommendations and not musts.

But you have to care to use everywhere the same name.

For example for Test_Model you have to:

Class Name

        class Test_Model extends CI_Model

File Name

        Test_Model.php

Load Model

        $this->load->model('Test_Model');

Use Model

        $this->Test_Model

To avoid using hard coding strings you can load model like this:

        $this->load->model(Test_Model::class);

Change your model file name, start with upper case letter like this: Logon_model.php

This happens when we migrate our project from windows server to LINUX server because linux is case sensitive while windows is not.

So, on windows even if don't write file name with upper case then also it will work fine but not on linux.


If you are on Linux then make sure your File name must match with the string passed in the load model methods the first argument.

$this->load->model('Order_Model','order_model');

You can use the second argument using that you can call methods from your model and it will work on Linux and windows as well

$result = $this->order_model->get_order_details($orderID);

I resolve this with this way:

  1. I rename file do Page_model.php
  2. Class name to Page_model extends...
  3. I call on autoload: $autoload['model'] = array('Page_model'=>'page');

Works fine.. I hope help.


I use codeigniter 3+. I had the same problem and in my case I changed model file name starting from uppser case.

Logon_model.php


Adding to @jakentus answer, below is what worked for me:

  1. Change the file name in the models package to Logon_model.php (First letter upper case as @jakentus correctly said)

  2. Change the class name as same as file name i.e.

    class Logon_model extends CI_Model

  3. Change the name in the load method too as

    $this->load->model('Logon_model');

Hope this helps. Happy coding. :)


you must change your model name first letter capital. in localhost small letter work properly but online this not work. for exa:

common_model.php

replaced it to

Common_model.php

Make sure:

  1. First letter uppercase
  2. Class name exact name as file name
  3. Make sure your file ends with .php extension

In my case I had 1 and 2 correct but forgot to name my file with .php extension. How I forgot, no idea but it sure gave me a hard time trying to figure out the problem


I experienced the same problem, but I fixed it by altering my application/config/routes.php file.

I made some restructuring to my controller directories and forget to effect it on the routes file.

Earlier:

$route['application/apply'] = 'ex/application/account/create';

and now:

$route['application/apply'] = 'application/account/create';


In CodeIgniter 3.0-dev (get it from github) this is not working because the CI is search as first letter uppercase.

You can find the code on system/core/Loader.php line 282 or bellow:

$model = ucfirst(strtolower($model));

foreach ($this->_ci_model_paths as $mod_path)
{
    if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
    {
        continue;
    }

    require_once($mod_path.'models/'.$path.$model.'.php');

    $CI->$name = new $model();
    $this->_ci_models[] = $name;
    return $this;
}

This mean that we need to create the file with the following name on application/models/Logon_mode.php


Just adding my problem i had:

$this->load->model("planning/plan_model.php");

and the .php shouldnt be there, so it should have been:

$this->load->model("planning/plan_model");

hope this helps someone


Here is what a model should look like: Make sure yours is like this.

    <?php
    class Logon_model extends CI_Model {

    function __construct()
    {
         parent::__construct();
    }

    function myFunc()
    {
      // do something
    }
}

note the upper-case class name.

To load it use:

$this->load->model('logon_model');

note all lower case.


Models must be named and called with the first letter of the model name capitalized and the rest in lowercase.

For example: $this->load->model('Logon_model');

and:

class Logon_model extends CI_Model {
...

But you are correct about the file name.


Changing the name of model name starting with Uppercase works. Example : Login_model.php instead of login_model.php


First letter of file name and class name must be in Uppercase.

Your model class will be

class Logon_model extends CI_Model

and the file name will be Logon_model.php

Access it from your contoller like

$this->load->model('Logon_model');


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to model

Eloquent ORM laravel 5 Get Array of ids Tensorflow: how to save/restore a model? Cannot overwrite model once compiled Mongoose AngularJS - Binding radio buttons to models with boolean values Accessing MVC's model property from Javascript MVC 4 - how do I pass model data to a partial view? How to load json into my angular.js ng-model? Check if an object exists CodeIgniter PHP Model Access "Unable to locate the model you have specified" Removing a model in rails (reverse of "rails g model Title...")

Examples related to codeigniter-2

Only variable references should be returned by reference - Codeigniter Codeigniter: does $this->db->last_query(); execute a query? Using Mysql WHERE IN clause in codeigniter CodeIgniter PHP Model Access "Unable to locate the model you have specified" Where do I put image files, css, js, etc. in Codeigniter? CodeIgniter: How to get Controller, Action, URL information