Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]
like image 677
Lance Avatar asked Feb 14 '26 07:02

Lance


1 Answers

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.

like image 159
Wayne Tun Myint Avatar answered Feb 16 '26 21:02

Wayne Tun Myint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!