Codeigniter类的执行行为

时间:2012-05-27 08:16:04

标签: php ajax codeigniter

我正在使用MY_Controller来处理登录功能,问题是我不知道如何在发生AJAX请求时阻止MY_Controller被扩展。似乎redirect()将使用非AJAX请求停止子类,是否可以停止子类被执行以获取AJAX请求?

function __construct() {
    parent::__construct();

    if(!$this->input->is_ajax_request()) {

        if(!$this->session->userdata('userid')) {
            //Render public menu in view
            $this->load->view('header_public_view');
            if($this->login_required()) {
                //Possibly change to render authentication view rather than redirect
                $this->session->set_userdata('destination', $this->input->server('REQUEST_URI'));
                redirect('authentication/login');
            }
        }
        else {
            $this->load->model('user_model');
            $data['user'] = $this->user_model->get_user($this->session->userdata('userid'));
            $this->load->view('header_user_view', $data);
        }

    }
    else {

        if(!this->session->userdata('userid') && $this->login_required) {

            //send response telling client to redirect  

        }

    }
}

1 个答案:

答案 0 :(得分:0)

将所有AJAX函数放入一个单独的控制器中,该控制器不会放弃MY_Controller

function Ajax_controller extends CI_Controller
{
    //code
}