2013-07-04 80 views
3

在我的控制器(控制器/ user.php的),我有:笨不加載模型

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

在我的模型(模型/ user_model.php),我有

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

如果我從加載語句中刪除

or die("error"); 

我得到一個500內部服務器錯誤。

我檢查的config.php和這裏的一些信息

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

我還編輯.htaccess文件從URL刪除「的index.php」,使之清潔。

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] 
+0

本地主機,或測試/現場網站在線? – sinisake

+0

我現在正在測試這個地方。 MAMP – Lance

+0

縮小範圍 - 刪除'$ this-> load->模型('user_model')或死(「error」);'並且將echo'a';出口; 如果它給了你'一個',那麼你的問題與模型有關。接下來你應該檢查你的application/config/database.php的配置。你可以在$ this-> load-> database()後退出。 (加載助手之前),看看它是否是數據庫連接 – galchen

回答

3

據說是加載模型最佳實踐,在__construct庫(構造函數)

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

}

也請嘗試更改控制器名「用戶」到「用戶」 (沒有錯,但如果不工作,嘗試它)。命名衝突可能是。

+1

仍然不工作 – Lance

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

    } 
} 
+0

仍然不工作 – Lance

0

不要使用contruct方法模型,而不是遵循這樣的規則:

# 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 
    } 
} 

如果您需要加載模型只對特定的功能,然後加載該函數的模型,而不是隻在構造函數。在構造函數中加載模型使模型函數可用於所有控制器功能。

+0

仍然不工作 – Lance

0

這也是值得注意的是,當你加載模型它不會自動連接到數據庫,但如果你的第三個參數傳遞真,那麼它將

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

如果你知道你要加載模型很多,你也可以自動加載它在application/config/autoload.php文件中

+0

仍然無法正常工作 – Lance

-1

檢查你的文件名。他們是否擴展.php?我自己有這個問題,事實證明,我忘了添加.php擴展名