Ion Auth - 另一个控制器中的Create_User不起作用

时间:2015-09-07 07:23:01

标签: codeigniter ion-auth

我已经使用了“auth”控制器并将其复制并重命名为“site”。我已将对视图等的引用重命名为“site”。当我访问www.mysite / index.php / site / create_user时,表单加载正常。但是,在点击提交时,我会被重定向到www.mysite.com/index.php/site/login,并且没有任何内容添加到数据库中。谁能告诉我为什么这不起作用?我的站点控制器如下:

<?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控制器中有效。在网站控制器中,我做它所以你必须登录,你必须是一个管理员来建立一个用户(即取消注释这一行//重定向('网站/登录','刷新');)然后它也有效,但出于某种原因,当该行被注释时,它在auth控制器中工作,但不在站点控制器中工作。

非常感谢任何帮助。我试图找出它但却看不出为什么它在一个而不是另一个中起作用(以及为什么它在网站中起作用但仅作为管理员在该代码被取消注释时并且在注释时根本不起作用,而在auth它适用于任何一种情况)。

提前致谢。

1 个答案:

答案 0 :(得分:1)

您被重定向的原因有两个原因。

首先:$('#nameSection'+profID).show().insertAfter(this); ... $("#nameDisplay"+profID).empty().append(data);

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

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

Auth控制器中的创建用户功能仅供管理员使用,您必须if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())输出代码,否则您将被重定向到登录页面,因为没有记录而不是管理员。

试试这个:

//

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

通过标记这两行,您应该能够在不重定向的情况下查看并提交页面。 :)