2012-08-09 88 views
0

我正在寫我的第一個CodeIgniter腳本,我無法獲得以下模型加載,如果有人可以幫我嗎?CodeIgniter模型加載不正確

這裏是我的控制文件:

public function process(){ 
// Load the model 
$this->load->model('users/login', 'users'); 

// Validate the user can login 
$result = $this->users->validate(); 

// Now we verify the result 
if(!$result){ 
    // If user did not validate, then show them login page again 
    $this->index(); 
}else{ 
    // If user did validate, 
    // Send them to members area 
    redirect('home'); 
}  
} 

這裏是我的模型

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 


class Login_Model extends CI_Model{ 
    function __construct() 
    { 
     // Call the Model constructor 
     parent::__construct(); 
    } 

    public function validate(){ 

     // grab user input 
     $username = $this->security->xss_clean($this->input->post('username')); 
     $password = $this->security->xss_clean($this->input->post('password')); 

     // Prep the query 
     $this->db->where('username', $username); 
     $this->db->where('password', $password); 

     // Run the query 
     $query = $this->db->get('users'); 
     // Let's check if there are any results 
     if($query->num_rows == 1) 
     { 
      // If there is a user, then create session data 
      $row = $query->row(); 
      $data = array(
        'userid' => $row->userid, 
        'fname' => $row->fname, 
        'lname' => $row->lname, 
        'username' => $row->username, 
        'validated' => true 
        ); 
      $this->session->set_userdata($data); 
      return true; 
     } 
     // If the previous process did not validate 
     // then return false. 
     return false; 
    } 
} 
?> 

我可以確認的過程中的作用卻是加載就是那張在$結果= $結果下方的任何代碼= $ this-> users-> validate();不會出現。該模型也在加載,只要我嘗試調用一個函數,腳本就會自殺。

對不起,如果這個問題有點平淡。

感謝 彼得

+0

也許這個問題正在發生,你在模型中使用了安全庫的bcs,但是沒有代碼來加載它。 – tijs 2012-08-09 16:40:33

+0

我使用此代碼加載模型$ this-> load-> model('users/login','users');它在控制器部分上方張貼。該模型似乎正在加載,腳本在達到$ result = $ this-> users-> validate()時自殺。 – 2012-08-09 16:42:38

+0

你可以使用'$ this-> input-> post('some_data',TRUE);'//第二個可選參數允許你通過XSS過濾器運行數據。通過將第二個參數設置爲布爾值TRUE來啓用它; – TigerTiger 2012-08-09 16:48:09

回答

1

這一切都來到了我的代碼。您的模型類名稱必須與模型文件的名稱相同。所以在這種情況下,我應該命名我的文件login_model.php,然後該類本身必須被稱爲Login_model(第一個字符必須是大寫字母,其他所有字符都必須是小寫字母)。當調用控制器中的模型必須全部是小寫字母,如:

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

希望這有助於任何人在未來,感謝所有爲您嘗試大寫的努力和評論:)

+2

作爲一個補充說明,您的類名可以是任何類型的大寫/小寫混合,但CodeIgniter將返回一個名爲object例如,如果你的類被稱爲LoginModel,你必須使用'$ this-> loginmodel'來調用它,這也適用於加載庫 – 2012-08-09 22:10:01

+0

感謝您的更新chris: ) – 2012-08-09 23:32:08

0

你的檔案名稱? user_model.php到User_model.php?