Php 从codeigniter中的控制器访问已发布的变量
我对codeigniter非常陌生,我正在构建一些示例工具来了解我的方法。我在网上学习了一些基本的教程,现在我要走自己的路了 我有下面的代码,我试图在注册用户之前确定用户是否存在。我也不知道如何告诉我的视图用户已经存在,如果是错误,我在哪里传回数据 我得到的错误是: 致命错误:第18行的/Users/Tom/www/crm/application/helpers/site_helper.php中不在对象上下文中使用$this controllers/users.phpPhp 从codeigniter中的控制器访问已发布的变量,php,codeigniter,Php,Codeigniter,我对codeigniter非常陌生,我正在构建一些示例工具来了解我的方法。我在网上学习了一些基本的教程,现在我要走自己的路了 我有下面的代码,我试图在注册用户之前确定用户是否存在。我也不知道如何告诉我的视图用户已经存在,如果是错误,我在哪里传回数据 我得到的错误是: 致命错误:第18行的/Users/Tom/www/crm/application/helpers/site_helper.php中不在对象上下文中使用$this controllers/users.php public functi
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');
}
}
}
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;
}
}
}
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);
}
helpers/site\u helper.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');
}
}
}
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;
}
}
}
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);
}
models/Users\u model.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');
}
}
}
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;
}
}
}
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);
}
$此
是对控制器对象实例的引用。您不能在助手函数中直接引用$this
。您可以使用get_instance
helper函数访问当前运行的控制器实例的实例
长话短说,请更新您的站点\u助手:
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;
}
}
}
有关更多信息,请查看:
看看这篇文章是否有用:它是为了视图而不是帮助者,但问题是相同的。