我正在尝试运行Ion Auth 2登录表单并在单个页面(我的主页)上注册用户表单,但没有成功。我试图在login()函数中从create_account()函数中公开代码,但它不起作用。我搜索了很多这样的例子,但是在一个页面上找不到这两个表格。有人可以给我一个建议或参考吗?非常感谢你提前!
这很容易。您有两种形式,每种形式都通过表单的action
属性指向不同的控制器功能。那些控制器功能(让我们称之为login
和register
设置flash消息并重定向回index
auth控制器(例如你的主页登录并创建用户表单)。
以下是使用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
}
}