在单页面实现CodeIgniter Ion Auth 2登录与注册功能
哥们,我之前也碰到过类似的问题,折腾了一阵终于搞定了,给你分享下在单页面集成Ion Auth登录和注册表单的可行方案:
核心思路
别把两个函数的代码硬凑在一起,而是在同一个控制器方法里,通过表单提交按钮的name属性来区分是登录请求还是注册请求,分别调用Ion Auth的官方方法处理,这样既清晰又不容易出错。
控制器代码实现
假设你的首页控制器是Home.php,可以这样写index方法:
public function index() { // 加载依赖库 $this->load->library('ion_auth'); $this->load->library('form_validation'); $data = []; // 处理登录提交 if ($this->input->post('login_submit')) { // 设置登录验证规则 $this->form_validation->set_rules('identity', '账号/邮箱', 'required'); $this->form_validation->set_rules('password', '密码', 'required'); if ($this->form_validation->run() === true) { $remember = (bool)$this->input->post('remember'); // 调用Ion Auth的登录方法 if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember)) { redirect('dashboard', 'refresh'); // 登录成功跳转到后台 } else { $data['login_error'] = $this->ion_auth->errors(); // 传递登录错误信息 } } } // 处理注册提交 if ($this->input->post('register_submit')) { // 设置注册验证规则(参考Ion Auth默认的注册规则) $this->form_validation->set_rules('username', '用户名', 'required|is_unique[users.username]'); $this->form_validation->set_rules('email', '邮箱', 'required|valid_email|is_unique[users.email]'); $this->form_validation->set_rules('password', '密码', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']'); $this->form_validation->set_rules('password_confirm', '确认密码', 'required|matches[password]'); if ($this->form_validation->run() === true) { $username = $this->input->post('username'); $email = $this->input->post('email'); $password = $this->input->post('password'); // 可选的额外用户信息 $additional_data = [ 'first_name' => $this->input->post('first_name'), 'last_name' => $this->input->post('last_name'), ]; // 调用Ion Auth的注册方法 if ($this->ion_auth->register($username, $password, $email, $additional_data)) { $data['register_success'] = $this->ion_auth->messages(); // 注册成功提示 } else { $data['register_error'] = $this->ion_auth->errors(); // 注册错误信息 } } } // 加载首页视图,传递状态信息 $this->load->view('home/index', $data); }
视图表单结构
在首页视图(比如home/index.php)里,给两个表单设置不同的提交按钮name属性,这样控制器能识别是哪个表单提交的:
<div class="container"> <!-- 登录表单区域 --> <div class="login-box"> <h3>用户登录</h3> <?php if (isset($login_error)): ?> <div class="alert alert-danger"><?php echo $login_error; ?></div> <?php endif; ?> <?php echo form_open(current_url()); ?> <div class="form-group"> <label>账号/邮箱</label> <input type="text" name="identity" class="form-control" value="<?php echo set_value('identity'); ?>"> <?php echo form_error('identity'); ?> </div> <div class="form-group"> <label>密码</label> <input type="password" name="password" class="form-control"> <?php echo form_error('password'); ?> </div> <div class="form-group"> <input type="checkbox" name="remember" value="1"> 记住我 </div> <button type="submit" name="login_submit" class="btn btn-primary">登录</button> <?php echo form_close(); ?> </div> <!-- 注册表单区域 --> <div class="register-box"> <h3>新用户注册</h3> <?php if (isset($register_success)): ?> <div class="alert alert-success"><?php echo $register_success; ?></div> <?php endif; ?> <?php if (isset($register_error)): ?> <div class="alert alert-danger"><?php echo $register_error; ?></div> <?php endif; ?> <?php echo form_open(current_url()); ?> <div class="form-group"> <label>用户名</label> <input type="text" name="username" class="form-control" value="<?php echo set_value('username'); ?>"> <?php echo form_error('username'); ?> </div> <div class="form-group"> <label>邮箱</label> <input type="email" name="email" class="form-control" value="<?php echo set_value('email'); ?>"> <?php echo form_error('email'); ?> </div> <div class="form-group"> <label>密码</label> <input type="password" name="password" class="form-control"> <?php echo form_error('password'); ?> </div> <div class="form-group"> <label>确认密码</label> <input type="password" name="password_confirm" class="form-control"> <?php echo form_error('password_confirm'); ?> </div> <button type="submit" name="register_submit" class="btn btn-success">注册</button> <?php echo form_close(); ?> </div> </div>
重要注意事项
- 不要直接复制
create_account()的代码到login()里,直接调用Ion Auth封装好的login()和register()方法,能避免很多自定义代码的bug。 - 两个表单的提交目标都是当前页面(
current_url()),这样提交后还能留在首页展示状态信息。 - 登录和注册的表单验证规则要分开设置,别混用,不然会出现验证逻辑混乱的问题。
- 错误/成功信息要分别存储传递,避免互相覆盖导致页面展示错误。
内容的提问来源于stack exchange,提问作者Liam James
相关产品推荐
相关产品推荐

