Razvan Zamfir
Razvan Zamfir

Reputation: 4712

Codeigniter 3: Undefined property: Controllername::$load

I am working on a Registration & Login system in CI3.

My controller is is made according to the CI user guide:

class Signin extends CI_Controller {

 public function index()
 {
    $this->load->view('signin');
 }

 public function signin()
 {  
    $this->form_validation->set_rules('email', 'Email', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required');
    if ($this->form_validation->run())
    {
        echo "You are in";
    }
    else
    {
        echo validation_errors();
    }
 }
}

I have loaded the libraries and helpers in autoload.php:

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

$autoload['helper'] = array('url', 'form');

Yet I get this error:

Message: Undefined property: Signin::$load
Filename: controllers/Signin.php

Why is this? Thank you!

Upvotes: 0

Views: 507

Answers (3)

Vickel
Vickel

Reputation: 8007

you need a constructor for your controller Signin in order to be able to use function signin (same name):

class Signin extends CI_Controller{
    public function __construct()
    {
        parent::__construct();
    }
    public function signin()
   { 
      // your code
   }
   public function index()
   {
      $this->load->view('signin');
   }
}

see CI-manual about reserved method names

Upvotes: 2

DFriend
DFriend

Reputation: 8964

The problem is that you have a class method with the same name as the class.

Per the documentation

You should also never have a method named identically to its class name. If you do, and there is no __construct() method in the same class, then your e.g. Index::index() method will be executed as a class constructor! This is a PHP4 backwards-compatibility feature.

So the fix is to do one of the following

  1. Change the definition of public function signin() to something else
  2. Add a __construct method to the class.
  3. Change the name of the class

I would use the first solution. Could be done as easily as

public function sign_in()
{

Upvotes: 0

Hassnain Abbas
Hassnain Abbas

Reputation: 51

You just need to add a constructor in your Signin controller.

class Signin extends CI_Controller {

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

    public function index()
    {
        $this->load->view('signin');
    }

    public function signin()
    {  
        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        if ($this->form_validation->run())
        {
            echo "You are in";
        }
        else
        {
            echo validation_errors();
        }
    }
}

Upvotes: 0

Related Questions