3

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]
6
  • Localhost, or test/live site online? Commented Jul 4, 2013 at 23:38
  • I'm testing this locally right now. MAMP Commented Jul 4, 2013 at 23:40
  • narrow it down - remove the $this->load->model('user_model')or die("error"); and put echo 'a'; exit; If it gives you 'a' then your problem is related to the model. The next thing you should do if check your application/config/database.php for configuration. You can exit after $this->load->database(); (before loading the helper) to see if it is the database connection Commented Jul 4, 2013 at 23:41
  • Turn error reporting on, you shouldn't develop in an environment that shows 500 error pages... Commented Jul 4, 2013 at 23:44
  • Tried your advice, Galchen. It doesn't give me 'a'. Commented Jul 4, 2013 at 23:46

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0
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() 
    {       

    }
}

Comments

0

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.

Comments

0

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

Comments

-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.