2015-09-07 39 views
1

我已将“auth”控制器复制并将其重命名为“site”。我已将视图的引用重命名为“网站”。当我去www.mysite/index.php/site/create_user时,表单加载正常。然而,在提交时,我将重定向到www.mysite.com/index.php/site/login,并且没有任何内容添加到数据库中。谁能告诉我为什么这不起作用?我的网站控制器低于:Ion Auth - Create_User在另一个控制器中不起作用

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 

class Site extends CI_Controller { 

// 
//Authentication 
// 

function __construct() 
{ 
    parent::__construct(); 
    $this->load->database(); 
    $this->load->library(array('ion_auth','form_validation')); 
    $this->load->helper(array('url','language')); 

    $this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth')); 

    $this->lang->load('auth'); 
} 


//Function to log the user in  
function login() 
{ 
    $this->data['title'] = "Login"; 

    //validate form input 
    $this->form_validation->set_rules('identity', 'Identity', 'required'); 
    $this->form_validation->set_rules('password', 'Password', 'required'); 

    if ($this->form_validation->run() == true) 
    { 
     // check to see if the user is logging in 
     // check for "remember me" 
     $remember = (bool) $this->input->post('remember'); 

     if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember)) 
     { 
      //if the login is successful 
      //redirect them back to the home page 
      $this->session->set_flashdata('message', $this->ion_auth->messages()); 
      redirect('/', 'refresh'); 
     } 
     else 
     { 
      // if the login was un-successful 
      // redirect them back to the login page 
      $this->session->set_flashdata('message', $this->ion_auth->errors()); 
      redirect('site/login', 'refresh'); // use redirects instead of loading views for compatibility with MY_Controller libraries 
     } 
    } 
    else 
    { 
     // the user is not logging in so display the login page 
     // set the flash data error message if there is one 
     $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message'); 

     $this->data['identity'] = array('name' => 'identity', 
      'id' => 'identity', 
      'type' => 'text', 
      'value' => $this->form_validation->set_value('identity'), 
     ); 
     $this->data['password'] = array('name' => 'password', 
      'id' => 'password', 
      'type' => 'password', 
     ); 

     $this->_render_page('site/login', $this->data); 
    } 
} 

//Function to log the user out 
function logout() 
{ 
    $this->data['title'] = "Logout"; 

    // log the user out 
    $logout = $this->ion_auth->logout(); 

    // redirect them to the login page 
    $this->session->set_flashdata('message', $this->ion_auth->messages()); 
    redirect('site/login', 'refresh'); 
} 


//Function to create a user 
function create_user() 
{ 
    $this->data['title'] = "Create User"; 

    if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()) 
    { 
     //redirect('site/login', 'refresh'); 
    } 

    $tables = $this->config->item('tables','ion_auth'); 

    // validate form input 
    $this->form_validation->set_rules('first_name', $this->lang->line('create_user_validation_fname_label'), 'required'); 
    $this->form_validation->set_rules('last_name', $this->lang->line('create_user_validation_lname_label'), 'required'); 
    $this->form_validation->set_rules('email', $this->lang->line('create_user_validation_email_label'), 'required|valid_email|is_unique['.$tables['users'].'.email]'); 
    $this->form_validation->set_rules('phone', $this->lang->line('create_user_validation_phone_label'), 'required'); 
    $this->form_validation->set_rules('company', $this->lang->line('create_user_validation_company_label'), 'required'); 
    $this->form_validation->set_rules('password', $this->lang->line('create_user_validation_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]'); 
    $this->form_validation->set_rules('password_confirm', $this->lang->line('create_user_validation_password_confirm_label'), 'required'); 

    if ($this->form_validation->run() == true) 
    { 
     $username = strtolower($this->input->post('first_name')) . ' ' . strtolower($this->input->post('last_name')); 
     $email = strtolower($this->input->post('email')); 
     $password = $this->input->post('password'); 

     $additional_data = array(
      'first_name' => $this->input->post('first_name'), 
      'last_name' => $this->input->post('last_name'), 
      'company' => $this->input->post('company'), 
      'phone'  => $this->input->post('phone'), 
     ); 
    } 
    if ($this->form_validation->run() == true && $this->ion_auth->register($username, $password, $email, $additional_data)) 
    { 
     // check to see if we are creating the user 
     // redirect them back to the admin page 
     $this->session->set_flashdata('message', $this->ion_auth->messages()); 
     redirect("site", 'refresh'); 
    } 
    else 
    { 
     // display the create user form 
     // set the flash data error message if there is one 
     $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message'))); 

     $this->data['first_name'] = array(
      'name' => 'first_name', 
      'id' => 'first_name', 
      'type' => 'text', 
      'value' => $this->form_validation->set_value('first_name'), 
     ); 
     $this->data['last_name'] = array(
      'name' => 'last_name', 
      'id' => 'last_name', 
      'type' => 'text', 
      'value' => $this->form_validation->set_value('last_name'), 
     ); 
     $this->data['email'] = array(
      'name' => 'email', 
      'id' => 'email', 
      'type' => 'text', 
      'value' => $this->form_validation->set_value('email'), 
     ); 
     $this->data['company'] = array(
      'name' => 'company', 
      'id' => 'company', 
      'type' => 'text', 
      'value' => $this->form_validation->set_value('company'), 
     ); 
     $this->data['phone'] = array(
      'name' => 'phone', 
      'id' => 'phone', 
      'type' => 'text', 
      'value' => $this->form_validation->set_value('phone'), 
     ); 
     $this->data['password'] = array(
      'name' => 'password', 
      'id' => 'password', 
      'type' => 'password', 
      'value' => $this->form_validation->set_value('password'), 
     ); 
     $this->data['password_confirm'] = array(
      'name' => 'password_confirm', 
      'id' => 'password_confirm', 
      'type' => 'password', 
      'value' => $this->form_validation->set_value('password_confirm'), 
     ); 

     $this->_render_page('site/create_user', $this->data); 
    } 
} 


//Function to render the page 
function _render_page($view, $data=null, $returnhtml=false)//I think this makes more sense 
{ 

    $this->viewdata = (empty($data)) ? $this->data: $data; 

    $view_html = $this->load->view($view, $this->viewdata, $returnhtml); 

    if ($returnhtml) return $view_html;//This will return html on 3rd argument being true 
} 

}

这个确切的代码工作的权威性控制器时。当在站点控制器中,我做到了,所以你必须登录,你必须是一个管理员,使用户(即取消注释此行/ /重定向('网站/登录','刷新');)然后它也可以,但出于某种原因,当该行被评论时,它在auth控制器中工作,但不在站点控制器中工作。

任何帮助,非常感谢。我试图找出它,但不明白为什么它在一个而不是其他(为什么它在网站中工作,但只作为管理员,当代码未注释,而不是当它被评论时,而在无论在哪种情况下都可以使用)。

在此先感谢。

回答

1

您重定向的原因是两个原因之一。

第一:$this->_render_page('site/login', $this->data);

当你点击提交按钮,它仍然指向到登录控制器。

二:if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())

在验证控制器创建用户功能仅用于管理员,你将不得不//出来的代码,否则你会被重定向到登录页面,由于没有被记录的,而不是作为一个管理员。

试试这个:

//$this->_render_page('site/login', $this->data);

//if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())

通过标记出这两条线,你应该能够在不被重定向到教职员,并提交你的页面。 :)

相关问题