【问题标题】:Ion Auth - Create_User In Another Controller Does Not WorkIon Auth - 另一个控制器中的 Create_User 不起作用
【发布时间】:2015-09-07 07:23:00
【问题描述】:

我已将“auth”控制器复制并重命名为“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
}

}

这个确切的代码在身份验证控制器中有效。当我在站点控制器中创建它时,您必须登录并且您必须是管理员才能创建用户(即取消注释此行 //redirect('site/login', 'refresh');)然后它也可以工作,但是出于某种原因,当该行被注释时,它在身份验证控制器中起作用,但在站点控制器中不起作用。

非常感谢任何帮助。我试图弄清楚,但看不出为什么它在一个而不是另一个中起作用(以及为什么它在站点中起作用,但仅在未注释该代码时才作为管理员,而在注释时则根本没有,而在验证它在任何一种情况下都有效)。

提前致谢。

【问题讨论】:

    标签: codeigniter ion-auth


    【解决方案1】:

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

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

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

    第二个:if (!$this-&gt;ion_auth-&gt;logged_in() || !$this-&gt;ion_auth-&gt;is_admin())

    Auth 控制器中的创建用户功能仅供管理员使用,您必须// 输出代码,否则由于未登录且不是管理员,您将被重定向到登录页面。

    试试这个:

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

    //if (!$this-&gt;ion_auth-&gt;logged_in() || !$this-&gt;ion_auth-&gt;is_admin())

    通过标记这两行,您应该能够查看和提交您的页面而不会被重定向。 :)

    【讨论】:

      猜你喜欢
      • 2013-02-15
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      相关资源
      最近更新 更多