php CodeIgniter 错误 - 在 null 上调用成员函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41988086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:31:42  来源:igfitidea点击:

CodeIgniter error - Call to a member function on null

phpcodeigniternull

提问by Vandan Kumbhat

I have my form which loads login method of Auth controller class on submit and I have AuthData model which has an authenticate function. Now when user submits form I get error?

我有我的表单,它在提交时加载 Auth 控制器类的登录方法,我有一个具有身份验证功能的 AuthData 模型。现在当用户提交表单时出现错误?

Warning is-

警告是——

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Auth::$AuthData
Filename: controllers/Auth.php Line Number: 21

Backtrace:
File: C:\xampp\htdocs\trials\application\controllers\Auth.php Line: 21 Function: _error_handler
File: C:\xampp\htdocs\trials\index.php Line: 315 Function: require_once

Error An uncaught Exception was encountered
Type: Error
Message: Call to a member function authenticate() on null
Filename: C:\xampp\htdocs\trials\application\controllers\Auth.php Line Number: 21

Backtrace:
File: C:\xampp\htdocs\trials\index.php Line: 315 Function: require_once

遇到 PHP 错误
严重性:通知
消息:未定义的属性:Auth::$AuthData
文件名:controllers/Auth.php 行号:21

回溯:
文件:C:\xampp\htdocs\trials\application\controllers\Auth.php 行:21 函数:_error_handler
文件:C:\xampp\htdocs\trials\index.php 行:315 函数:require_once

错误遇到未捕获的异常
类型:错误
消息:调用成员函数authenticate() on null
文件名:C:\xampp\htdocs\trials\application\controllers\Auth.php 行号:21

回溯:
文件:C:\xampp\htdocs\trials\index.php 行:315 功能:require_once

Here is my controller

这是我的控制器

<?php
//defined('BASEPATH') OR exit('No direct script access allowed');

class Auth extends CI_Controller {
public function index()
{
    $this->load->view('welcome_message');
    $this->load->model('AuthData');
    $this->load->helper('url');
    // needed ???
    $this->load->database();
}

public function login()
    {
        $user = $this->input->post('username');
        $pass = $this->input->post('password');
        $input = array( 'username'=>$user, 'password'=>$pass);

        $data['loggedIn'] = "no";
        $chk = $this->AuthData->authenticate($input);
        if($chk)
            redirect('Sample/success');
        else
            redirect('Sample/failed');


        //$this->load->view('home/contact');
    }   
}

My model

我的模型

<?php
//session_start();
class AuthData extends CI_Model {

    public function __construct()
    {
            $this->load->database();
    }

    public function authenticate($input=NULL)
    {
        $query = $this->db->query('SELECT * FROM user');
        $rows = $query->result_array();
        foreach($rows as $check)
        {
            if($input['password']==$check['password'])
            {
                if($input['username']==$check['usernmae'])
                {
                    //session_start();
                    $_SESSION['login'] = "T";
                    $_SESSION['username'] = $input['username'];
                    //is it unsafe to keep password?
                    $_SESSION['password'] = $input['password'];
                    return true;
                    //maybe break here is redundant,but dont want risk
                    break;
                }
            }
        }
        return false;
    }   
}
?>

And form_open in view

和 form_open 视图

<?php echo form_open('auth/login', ['class' => 'form-signin', 'role' => 'form']); ?>

Also if it matters, I have removed index.php.

另外,如果重要的话,我已经删除了 index.php。

回答by Antony

As @aynber correctly states the AuthData model isn't available globally in this function.

正如@aynber 正确指出的那样,此函数中的 AuthData 模型不是全局可用的。

Your 2 options are to either: 1 - autoload it in the config/autoload.php script as @wolfgang1983 states or 2: setup a constructor

您的 2 个选项是:1 - 在 config/autoload.php 脚本中自动加载它,如@wolfgang1983 所述或 2:设置一个构造函数

public function __construct(){
$this->load->model('Authdata');
}

It will then be available throughout your controller.

然后它将在您的整个控制器中可用。