public function index() {
if ($this->session->userdata('admin_login') == 1)
redirect(base_url() . 'index.php?admin/admin_dashboard', 'refresh');
if ($this->session->userdata('teacher_login') == 1)
redirect(base_url() . 'index.php?teacher/teacher_dashboard', 'refresh');
if ($this->session->userdata('student_login') == 1)
redirect(base_url() . 'index.php?student/student_dashboard', 'refresh');
if ($this->session->userdata('parent_login') == 1)
redirect(base_url() . 'index.php?parents/parents_dashboard', 'refresh');
$this->load->view('backend/login');
}
function ajax_login() {
$response = array();
$email = $_POST["email"];
$password = sha1($_POST["password"]);
$response['submitted_data'] = $_POST;
$login_status = $this->validate_login($email, $password);
$response['login_status'] = $login_status;
if ($login_status == 'success') {
$response['redirect_url'] = '';
}
echo json_encode($response);
}
我想通过$ response ['redirect_url'] =''传递index();如何通过?我已经尝试过通过创建路由来尝试,但无法正常工作。
答案 0 :(得分:0)
index()
是一个函数,根据您的代码,它可以重定向用户或显示页面。对于ajax响应而言,这都不是一件好事,因为重定向或视图只会在ajax请求中发生或呈现,而不会在用户的视口中呈现。
我假设您想在索引函数中添加逻辑,而不必重复太多。简单的解决方案是创建一个重定向功能。
创建一个助手并将其放入其中:
function group_redirect() {
$ci = &get_instance();
$ci->load->helper('url');
if ($ci->session->userdata('admin_login') == 1)
return base_url() . 'index.php?admin/admin_dashboard';
if ($ci->session->userdata('teacher_login') == 1)
return base_url() . 'index.php?teacher/teacher_dashboard';
if ($ci->session->userdata('student_login') == 1)
return base_url() . 'index.php?student/student_dashboard';
if ($ci->session->userdata('parent_login') == 1)
return base_url() . 'index.php?parents/parents_dashboard';
return false;
}
然后剩下的就很简单了:
public function __construct() {
$this->load->helper('name_of_helper_with_function');
}
public function index() {
$redir = group_redirect();
if ($redir) {
redirect($redir);
}
$this->load->view('backend/login');
}
function ajax_login() {
$response = array();
$email = $_POST["email"];
$password = sha1($_POST["password"]);
$response['submitted_data'] = $_POST;
$login_status = $this->validate_login($email, $password);
$response['login_status'] = $login_status;
if ($login_status == 'success') {
$response['redirect_url'] = $redir;
}
echo json_encode($response);
}