CodeIgniter - 在非对象上调用成员函数model()

时间:2013-07-16 23:31:26

标签: codeigniter php

我有一个在视图中创建的登录表单。然后在我的控制器中我试图加载我的模型,但似乎没有加载。在stackoverflow上查了几个答案,几乎每个人都说自动加载模型,导入数据库库等。我这样做但我仍然收到错误

    Fatal error: Call to a member function model() on a non-object in C:\xampp\htdocs\\project\application\controllers\login.php on line 11 

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined property: Login::$load

    Filename: controllers/login.php

    Line Number: 11

login_view

<?php
    $loginEmail = array('placeholder' => "Email", 'name' => "loginEmail");
    $loginPassword = array('placeholder' => "Wachtwoord", 'name' => "loginPassword");
    $loginSubmit = array('name' => "loginSubmit", 'class' => "btn", 'value' => "Inloggen");
    $loginForgot = array('name' => "loginForgot", 'class' => "link", 'value' => "Wachtwoord vergeten?");

    echo form_open('login/login', array('class' => 'grid-100 formc'));
    echo form_input($loginEmail);
    echo form_password($loginPassword);
    echo form_submit($loginSubmit);
    echo form_submit($loginForgot);
?>

login_controller

<?php

Class Login extends CI_Controller{
    function index(){
        $data['content'] = 'login_view';
        $this->load->view('templates/template', $data);
    }


    function login(){
        $this->load->model('login_model');
        $query = $this->login_model->validate();

        if($query){
            $data = array(
                'username' => $this->input->post('loginEmail'),
                'loggedin' => true

            );

            $this->session->set_userdata($data);
             redirect('profile/myprofile');
        }
    }

}

?>

login_model

<?php

Class Login_model extends CI_Model{

    function validate(){
        $this->db->where('email', $this->input->post('loginEmail'));
        $this->db->where('password', md5($this->input->post('loginPassword')));

        $query = $this->db->get('tbl_users');

        if($query->num_rows == 1){
            return true;
        }
    }

}

?>

autoload.php

$autoload['libraries'] = array('database', 'session');

我在这里缺少什么?

2 个答案:

答案 0 :(得分:17)

您的登录控制器中没有构造函数:

public function __construct() {        
    parent::__construct();
}

答案 1 :(得分:1)

根据CodeIgniter文档

  

如果您打算在任何控制器中使用构造函数,则必须在其中放置以下代码行:   父:: __构建体();

请参阅CI Constructor了解更多信息。