Lance
Lance

Reputation: 4820

CodeIgniter not loading model

In my controller (controllers/user.php), I have:

class User extends CI_Controller 
{
    public function index() 
    {       
         $this->load->model('user_model')or die("error");  
    }
}

In my model (models/user_model.php), I have

class User_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->helper('common');
    }
}

If I remove the

or die("error");

from the load statement, I get a 500 internal server error.

I checked config.php and here's some info

$config['base_url'] = ''; 
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';

I have also edited the .htaccess file to remove the "index.php" from the URL to make it cleaner.

RewriteEngine On
RewriteCond $1 !^(index.php|images|captcha|css|js|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Upvotes: 3

Views: 11263

Answers (5)

Wayne Johnson
Wayne Johnson

Reputation: 1

check your filenames. Are they extension .php?? I had this problem myself and it turned out that I was forgetting to add the .php extension

Upvotes: -1

Pattle
Pattle

Reputation: 6016

It is also worth noting that when you load a model it doesn't automatically connect to the database but if you pass TRUE as the third parameter then it will

class User extends CI_Controller 
{
    public function __construct() 
    {       
         parent:: __construct();
         $this->load->model('user_model', '', TRUE);  
    }
}

if you know you are going to be loading a model a lot you can also autoload it in the application/config/autoload.php file

Upvotes: 0

Nil'z
Nil'z

Reputation: 7475

Dont use the contruct method in the model instead follow this rule:

# in the User controller
class User extends CI_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->database();
        $this->load->helper('common');
        $this->load->model('user_model');
    }

    public function index()
    {       
         //$this->load->model('user_model')or die("error");
         #now you can use the user_model here 
    }
}

If you need to load the model only for a specific function then load the model in that function only instead of the constructor. Loading the model in the constructor makes the model functions available to all the controller function.

Upvotes: 0

Thanh Nguyen
Thanh Nguyen

Reputation: 5342

class User extends CI_Controller 
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->helper('common');
        $this->load->model('user_model');
    }    

    public function index() 
    {       

    }
}

Upvotes: 0

Wayne Tun Myint
Wayne Tun Myint

Reputation: 128

It is said to be a best practice to load models, libraries in __construct (constructor)

class User extends CI_Controller 
{
    public function __construct() 
    {       
         parent:: __construct();
         $this->load->model('user_model');  
    }

}

Please also try changing Controller name 'User' to 'Users' (not wrong but try it if not working). Naming conflict may be.

Upvotes: 3

Related Questions