在非目标代码点火器上调用成员函数run()

时间:2012-03-07 04:54:38

标签: codeigniter-2

我正在尝试使用代码点火器2.1.0设计用户注册表单。我在控制器的regitration.php中使用了以下代码来添加用户。

class Registration extends CI_Controller
{

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


    function index()
    {
        $data['main_content'] = 'registration';

        // Checks to see if form validation rules were met an executed properly.  If not, will return with registration form.
        if ($this->form_validation->run('registration') === FALSE) 
        {
            $data ['title'] = 'Registration';
            $this->load->view('include/template', $data);
        }

        // If validation passes, information will be passed along to the MODEL to be processed and the account will be created.
        else 
        {
            $this->load->model('registration_model');
            $this->registration_model->addUser();

            $this->session->set_flashdata('success', 'Your account has been successfully created');
            redirect(uri_string());
        }
    }
}

但它显示了Call to a member function run() on a non-object的错误。我该如何纠正?

1 个答案:

答案 0 :(得分:3)

请包含

// load 'form' helper
    $this->load->helper('form');

    // load 'validation' class
    $this->load->library('form_validation');

立即尝试

function __construct() {



    // load controller parent
   parent::__construct();   

    // load 'url' helper
    $this->load->helper('url');

    // load 'form' helper
    $this->load->helper('form');

// load 'session' 
 $this->load->library('session');
    // load 'validation' class
    $this->load->library('form_validation');

  }

好像$ session类没有正确初始化。

  1. 如果您正在使用数据库会话,请检查default_ci_sessions表。
  2. 检查构造函数是否正在加载会话库。
  3. 检查会话是否在autoload.php config
  4. 对于此"In order to use the Session class you are required to set an encryption key in your config file."。  将此添加到您的config.php

    $config['encryption_key'] = 'your_encryption_key_here';