【问题标题】:CodeIgniter error - Call to a member function on nullCodeIgniter 错误 - 在 null 上调用成员函数
【发布时间】:2017-06-18 16:34:06
【问题描述】:

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

警告是-

遇到 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

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

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

这是我的控制器

<?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');
    }   
}

我的模型

<?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;
    }   
}
?>

和 form_open 在视图中

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

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

【问题讨论】:

标签: php codeigniter null


【解决方案1】:

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

您的 2 个选项是:1 - 按照 @wolfgang1983 状态在 config/autoload.php 脚本中自动加载它或 2:设置构造函数

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

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

【讨论】:

  • 这是一个错误,一直以来我很明显我已经在我的构造函数中加载了 AuthData。无论如何感谢您的指出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
  • 2020-01-19
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 2019-02-08
相关资源
最近更新 更多