致命错误:在中的非对象上调用成员函数login()


Fatal error: Call to a member function login() on a non-object in

我作为项目继承的web应用程序出现问题,不幸的是我无法跟踪错误。模型似乎没有加载,但我可能错了。任何帮助都会很棒。

代码为:

public function login()
{
    //If request is post then user is trying to login, so process the login info
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        //Run the model method ::login
        $login_successful = $this->User->login();
        // check login status
        if($login_successful) {
            // if YES, then move user to dashboard/index (btw this is a browser-redirection, not a rendered view!)
            header('location: ' . URL . 'passwords/index');
        } else {
            echo "Incorrect user / pass combination entered. Please try again.";
        }

    }
}

模型函数为:

public function login() {
    $username = $_POST['data']['User']['username'];
    $password = $_POST['data']['User']['password'];

    $bind = array(
        ":username" => "$username",
    );
    $result = $this->select("users", "username = :username", $bind);
    //Check the password returned from the db against the password entered
    if (Bcrypt::checkPassword($password, $result[0]['password']) == true) {
        Session::init();
        Session::set('user_logged_in', true);
        Session::set('user_id', $result[0]['id']);
        Session::set('user_name', $result[0]['username']);
        Session::set('user_permission', $result[0]['permission']);
        Session::set('user_role', $result[0]['role']);
        return true;
    } else {
        return false;
    }
}

我还注意到控制器和模型都有一个名为login的函数。]

感谢

原因是$this->User不是有效的类实例。

确保它是一个对象。

尝试

var_dump($this->User);
die();
//Run the model method ::login
$login_successful = $this->User->login();

你会看到那里没有任何实例。

该怎么办

找到您期望初始化模型的位置。检查一下,为什么不是。