【问题标题】:Accessing posted variables from controller in codeigniter在 codeigniter 中从控制器访问发布的变量
【发布时间】:2015-04-05 18:28:52
【问题描述】:

我对 codeigniter 很陌生,我正在构建一些示例工具来了解我的方法。我已经在网上学习了一些基本教程,现在我要走自己的路了。

我有以下代码,我正在尝试在注册之前确定用户是否存在。我也不知道如何告诉我的视图用户已经存在,如果它是一个错误,我在哪里将数据传回?

我得到的错误是:

致命错误:在第 18 行的 /Users/Tom/www/crm/application/helpers/site_helper.php 中不在对象上下文中使用 $this

controllers/users.php

public function register()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $data['title'] = 'Create a new user';

        $this->form_validation->set_rules('firstname', 'First Name', 'required');
        $this->form_validation->set_rules('surname', 'Surname', 'required');

        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header', $data);
            $this->load->view('users/register');
            $this->load->view('templates/footer');

        }
        else
        {
            if(c_userexists($this->input->post('email'))){
                $this->load->view('templates/header', $data);
                $this->load->view('users/register');
                $this->load->view('templates/footer');
            } else {
                $this->users_model->set_register();
                $this->load->view('users/success');
            }
        }
    }

helpers/site_helper.php

   if(!function_exists('c_userexists'))
    {
        function c_userexists($value)
        {
            $this->db->select('count(*) as user_count');
            $this->db->from('users');
            $this->db->where('email', $userId);

            $query = $this->db->get();
            if($query > 0){
                return true;
            } else {
                return false;
            }
        }
    }

models/Users_model.php

public function set_register()
    {
        $this->load->helper('url');

        $data = array(
            'firstname' => $this->input->post('firstname'),
            'surname' => $this->input->post('surname'),
            'email' => $this->input->post('email'),
            'password' => c_passencode($this->input->post('email'))
        );

        return $this->db->insert('users', $data);
    }

【问题讨论】:

标签: php codeigniter


【解决方案1】:

$this 是对控制器对象实例的引用。你不能直接在你的辅助函数中引用$this。您可以使用get_instance 辅助函数来访问当前运行的控制器实例的实例。

长话短说,更新您的 site_helper:

if(!function_exists('c_userexists'))
{
    function c_userexists($value)
    {
        $CI =& get_instance();
        $CI->db->select('count(*) as user_count');
        $CI->db->from('users');
        $CI->db->where('email', $userId);

        $query = $CI->db->get();
        if($query > 0){
            return true;
        } else {
            return false;
        }
    }
}

如需更多信息,请查看: http://www.codeigniter.com/userguide3/general/ancillary_classes.html?highlight=get_instance#get_instance

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-16
    • 2011-01-17
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    相关资源
    最近更新 更多