Vandan Kumbhat
Vandan Kumbhat

Reputation: 25

CodeIgniter error - Call to a member function on null

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?

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

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

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

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

Upvotes: 2

Views: 75812

Answers (1)

Antony
Antony

Reputation: 4300

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

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

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

It will then be available throughout your controller.

Upvotes: 6

Related Questions