代码点火器离子身份验证 2 登录和注册在单个页面上



我正在尝试运行 Ion Auth 2 登录表单并在单个页面(我的主页(上注册用户表单,但没有成功。我试图在 login(( 函数中对 create_account(( 函数中的代码进行配音,但它不起作用。我搜索了很多这样的例子,但在一页上找不到两种表格。有人可以给我建议或推荐吗?提前非常感谢!

这很容易。您有两个窗体,每个窗体都通过窗体的 action 属性定向到不同的控制器函数。这些控制器功能(我们称它们为loginregister设置闪存消息并重定向回index身份验证控制器(例如,带有登录名和创建用户表单的主页(。

下面是一个使用 https://github.com/benedmunds/CodeIgniter-Ion-Auth/blob/2/controllers/Auth.php 中的一些代码的示例

<?php
class Auth extends CI_Controller {
    public function __construct() {
        parent::__construct();
        if ($this->ion_auth->logged_in()) {
            // user logged in, redirect them to the dashboard
            redirect('dashboard');
        }
    }
    /**
     * this page, /auth/ will have your forms and will
     * submit to login() and  register()
     * 
     * login form action: /auth/login
     * create account form action: /auth/register
     */
    public function index() {
        $data['message'] = $this->session->flashdata('message');
        $this->load->view('login_and_create_user', $data);
    }
    public function login() {
        // validate form input
        $this->form_validation->set_rules('identity', str_replace(':', '', $this->lang->line('login_identity_label')), 'required');
        $this->form_validation->set_rules('password', str_replace(':', '', $this->lang->line('login_password_label')), 'required');
        if ($this->form_validation->run()) {
            // 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)) {
                $this->session->set_flashdata('message', $this->ion_auth->messages());
                // if login is successful, redirect to dashboard
                redirect('dashboard');
            } else {
                // login failed
                $this->session->set_flashdata('message', $this->ion_auth->errors());
            }
        } else {
            // form validation failed, redirect to auth with validation errors
            $this->session->set_flashdata('message', validation_errors());
        }
        redirect('auth'); // redirect them back to the login/create user page
    }
    public function register() {
        // same thing here for create logic
        // validate, db, redirect
    }
}

最新更新